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.
Related
This question already has answers here:
How to slice an array in Bash
(4 answers)
Using ${1:1} in bash
(2 answers)
Bash: all of array except last element
(1 answer)
Closed 3 years ago.
May you please explain what does this mean:
#:1:$#-1
can you provide an exact explanation?
That's a parameter expansion that returns all the positional parameters but the last one.
It's based on ${parameter:offset:length}, where using # as parameter makes it work on positional parameters. The length $#-1 is the number of positional parameters ($#) minus one.
Here's a showcase : https://ideone.com/HKcaNj
This question already has answers here:
Why does String#gsub double content?
(3 answers)
Closed 5 years ago.
I have this code:
"1'2".gsub("'","\\'")
Instead of "1\'2", I get: "122". Why?
You need to use this:
puts "1'2".gsub("'","\\\\'")
It is because "\\'" means the context following the match, which is "2".
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 an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 8 years ago.
What does this Ruby regex mean?
/.../
This regex matches any three characters.
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.