Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Surprising string concatenation [duplicate]

Tags:

ruby

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.

like image 445
Bob Kuhar Avatar asked Oct 31 '22 18:10

Bob Kuhar


1 Answers

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.

like image 73
philip yoo Avatar answered Nov 15 '22 06:11

philip yoo