Surprising string concatenation [duplicate] - ruby

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.

Related

Using regular expressions via `re-search-forward` in elisp [duplicate]

This question already has answers here:
Emacs - regular expressions in Lisp need to be double-escaped - why?
(4 answers)
Closed 4 years ago.
I want to search for an regular expression with the function re-search-forward
When I tried using the examples from the page
here: https://www.emacswiki.org/emacs/RegularExpression#toc1
specifically the regular expression \w\{20,\} used to search for a word with 20 letters or more, I get an error.
Here I am placing my cursor after the closing parenthesis in my Lisp buffer and pressing C-x C-e for evaluating it.
However, when I use the Regexp I-search via,
C-M-s it highlights the correct word as expected.
Why is this?
This regexp:
\w\{20,\}
is expressed in a double-quoted elisp string like so:
"\\w\\{20,\\}"
Backslashes are special to the double-quoted read syntax for strings as well as being special to regexp syntax; so if a backslash is for the regexp, you need to double it.

Understanding Ruby match method of Regexp class [duplicate]

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.

Strange string behavior in Ruby [duplicate]

This question already has answers here:
Where is Ruby's string literal juxtaposition feature officially documented?
(4 answers)
Closed 7 years ago.
So I was playing around in Ruby, and noticed that "a""b" returns "ab". If fond this very strange and useless, so I was wondering what this is called and if it has a purpose. I would appreciate any answers. Thanks!
This is called "string literal concatenation" and it is common in many languages. More specifically, adjacent string literals that are not separated by any other operators are automatically concatenated together. They may be considered to be just one string literal.
This exists in C, C++, Python, and Ruby to name a few.
MSDN: String Literal Concatenation (C)
Lexical Analysis: String literal concatenation (Python)
Where is Ruby's string literal juxtaposition feature officially documented? (Ruby)
Literals - Strings (Ruby)
An example of where this might be used is to break up a long string onto multiple lies, also adding the ability to comment each piece. Something I wrote in Python the other day:
hdr = struct.Struct('<'
'8s' # 0x00 Magic value
'I' # 0x08 Offset
'I' # 0x0C Length
'H' # 0x10 Type
'H' # 0x12 Flags
) # 0x14 (Total)
Note that this method takes just one parameter, a string, and I didn't manually concatenate the pieces.
I've never noticed this before, and this looks to be another form of concatenation like << and +.

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.

How does string interpolation work without #{..} syntax? [duplicate]

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.

Resources