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.
Related
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 6 years ago.
I was reading about the match method in ruby, I understood most of the example given at Regexp
But I am failing to understand, why is:
/[0-9a-f]/.match('9f')
=> #<MatchData "9">
And not:
=> #<MatchData "9f">
I might be missing some basic understanding of Regex, so bear with me.
Because you're asking it to match a single character of class 0-9 or a-f.
If you want to match multiple use a plus or an asterisk after the character classes e.g. /[0-9a-f]+/.match('9f')
It's all here.
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:
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.
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 answers here:
Ruby's double colon (::) operator usage differences
(2 answers)
Closed 8 years ago.
I have seen some constants prefixed with double colons like
::DATE_FORMAT
What does this mean, and how are they different from the normal constants ?
It's the scope resolution operator. With an empty LHS, it is scoping to global.
It would be used when simply accessing the property/method/etc by its name alone would be ambiguous, e.g. a DATE_FORMAT available in scope which isn't the one you're after.