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.
Related
This question already has answers here:
Method to parse HTML document in Ruby?
(4 answers)
Closed 5 years ago.
html is string like:
<xxx name1 = "value1" name2 = "value2">
How to use regular expression to parse key-value pairs into a hash (like {"name1": "value1", "name2": "value2"})? The tag does not matter now.
I am new to ruby and have no idea how to start. Any hints? Thanks
UPDATE
My question is how to use regex to do parsing. I know that third party is a good option. But I am just curious about how to use regex to do parsing.
Regardless of the programming language, parsing html using regexp is not a good idea. In case of Ruby, Nokogiri is a good option.
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 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.
This question already has answers here:
ruby operator "=~" [duplicate]
(3 answers)
Closed 9 years ago.
I was wondering if anyone could explain what the =~ operator does in Ruby. I have seen it a few times but am unable to find a proper explanation of it.
It is used to match Regexes against strings:
http://www.ruby-doc.org/core-2.0.0/Regexp.html#method-i-3D-7E
It returns either a Integer value of the first occurrence in the string or if the expression doesn't match the String it returns nil.