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.
Related
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"]
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
This question already has answers here:
Where is Ruby's string literal juxtaposition feature officially documented?
(4 answers)
Closed 7 years ago.
I am surprised by some string concatenation I've stumbled upon in a codebase I support. Why, or how really, does the following manage to concatenate two strings together?
queue_name = 'gen-request-' "#{ENV['USERNAME'].gsub('.','')}"
=> "gen-request-robertkuhar"
I had expected to see a '+' between the two strings, but its not there. Is it implied or something?
I know this just makes more sense with up-the-middle string interpolation. Thats not what I'm asking. I want to know what it is about the language syntax that allows this to work in the first place.
This only works for string literals, and a part of the literal syntax.
If you have 2 string literals with just whitespace between them, they get turned into a single string. It's a convention borrowed from later versions of C.
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.
This question already has an answer here:
Why does string interpolation work in Ruby when there are no curly braces?
(1 answer)
Closed 9 years ago.
The code below is taken from page 118 of the Pickaxe book. Can someone explain to me why we do not need to do #{#name} to do interpolation?
class TaxCalculator
def get_tax(amount)
"##name on #{amount} = #{#block.call(amount)}"
end
end
When the expression to be interpolated is just a reference to a global, instance or class variable, then the braces can be omitted. The braces are only required for more complex expressions.
However, there is a debate about whether omitting the braces is a good idea from a style and readability perspective.
There is already a good answer to this question here:
Why does string interpolation work in Ruby when there are no curly braces?
In short: It's possible to spare the {} when you use a global, a class or an instance variable.