How to add a line in code for ruby [duplicate] - ruby

This question already has answers here:
Breaking up long strings on multiple lines in Ruby without stripping newlines
(8 answers)
Closed 6 years ago.
I apologise I am sure the answer is out there but I simply cannot articulate it well enough for google search..
Given a long piece of code
puts 'This is a really long line of ruby code here'
How can you separate it over 2 lines, i.e.
puts 'This is a really long
line of ruby code here'

str = 'first line'\
' second line'\
' third line'
puts str

Related

Yaml - how to break very long string [duplicate]

This question already has answers here:
How do I break a string in YAML over multiple lines?
(9 answers)
Closed 4 years ago.
I have an entry in my yaml file that looks like this
my_key:['short string', 'thisisaverylongstringthatcontains.,specialcharacterssoI havetousequotes,andI wanttobreakintomultiplelines']
My very long string can't contain spaces, and I am worried if I simply use newline, it will get converted to space.
What is the cleanest, simplest way to break down that second string across multiple lines for easier readability and convenience?
key : ['short string', "this is a very long string
that I want to break into
multiple lines"]
Have you tried just inserting a newline? This is valid YAML.
If you don't want spaces, add \ to the end of each line.
key : ['short string', "this is a very long string\
that I want to break into\
multiple lines"]

How do I remove strings in ruby that are between defined strings? [duplicate]

This question already has answers here:
How to return the substring of a string between two strings in Ruby?
(3 answers)
Closed 6 years ago.
My goal is to be able to define different sets of sub-strings that can be removed without eliminating the other strings. Open to better ideas.
What I have now is:
#outbound_text = " XTEST this is hidden XTEST hey there, what's up! XTEST this is also hidden XTEST but then I just keep writing here "
I tried the following but realized it was deleting hey there, what's up
if ENV['ENVIRONMENT'] == 'test'
# this will allow the XTEST string to come through
else # for production and development, remove the XTEST
unless #outbound_text.gsub!(/XTEST(.*)XTEST/ , '').nil?
#outbound_text.strip!
end
logger.debug "remove XTEST: #{#outbound_text}"
end
Open to different strings bookending what I need to remove (but the number of hidden sub-strings will vary so they can only be a beginning and end).
I think open to -- although have a number of them which get parsed, so open to using Nokogiri to remove the hidden tags. I would need to spend some time to try that, but wanted to know if there were a simple gsub before trying it.
Just make the repetition non-greedy:
#outbound_text.gsub(/XTEST(.*?)XTEST/ , '').strip
# => "hey there, what's up! but then I just keep writing here"

Not outputting #{variable} within a string [duplicate]

This question already has answers here:
Double vs single quotes
(7 answers)
Ruby - what's the difference between single and double quotes? [duplicate]
(1 answer)
Closed 8 years ago.
I cannot print an input variable as part of a string.
name = gets
>>> Lizzie
print #{name}
>>> Lizzie
That works. However, when I want to do:
name = gets
print 'Your name is: #{name}'
it outputs:
Your name is: #{name}
Any help would be appreciated.
Not sure why #peter_huene didn't post this as an answer, but just to complete this question the answer is:
name = gets
print "Your name is: #{name}"
You use double quotes to get variable injection and other features. Single quotes are literal quotes with no post processing.
#Lizzie - Ruby isn't "more sensitive" - it offers you two explicit ways of handling strings, which is more flexible. Unfortunately this means a bit of "magic" in that strings behave differently depending on how quoted, but it's a feature not a peculiarity.

How to split the string by certain amount of characters in Ruby [duplicate]

This question already has answers here:
Chop a string in Ruby into fixed length string ignoring (not considering/regardless) new line or space characters
(5 answers)
Closed 8 years ago.
How to split the string by certain amount of characters in Ruby?
For example, suppose that I have the following string:
some_string
and I want to split it by every 4th character, so the resulting strings will look like this:
som
e_s
tri
ng
How can I do it?
Thanks in advance.
Using Enumerable#each_slice
'some_string'.chars.each_slice(3).map(&:join)
# => ["som", "e_s", "tri", "ng"]
Using regular expression:
'some_string'.scan(/.{1,3}/)
# => ["som", "e_s", "tri", "ng"]

Ruby << for String [duplicate]

This question already has answers here:
What are <-- Ruby Strings called? And how do I insert variables in them?
(3 answers)
Closed 8 years ago.
Can someone tell me the name of
<<-MAP
STRING HERE
MAP
operator (<<-) in ruby? I tried search for 'double less than' but it didn't turn up anything. I want to learn more about it but don't even know what it's called!
Thanks
Thats called the here doc syntax .Generally used to enter multiline strings. You can read about it here http://blog.jayfields.com/2006/12/ruby-multiline-strings-here-doc-or.html
and also here The <<- operator on Ruby, where is it documented?
It's not an operator, it's a here document (aka heredoc) String literal. It works more or less like heredocs in other languages.
It is specified in section 8.7.6.3.6 of the ISO Ruby Language Specification.

Resources