5

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.

Bob Kuhar
  • 10,838
  • 11
  • 62
  • 115
  • 2
    https://stackoverflow.com/questions/18193792/where-is-rubys-string-literal-juxtaposition-feature-officially-documented – cremno Sep 30 '15 at 23:52
  • While the answer is the same, I don't think this is a duplicate of the "where is juxtaposition documented" question. – Bob Kuhar Oct 01 '15 at 23:26

1 Answers1

2

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.

philip yoo
  • 2,462
  • 5
  • 22
  • 37
  • Oh my. You learn something new everyday. https://stackoverflow.com/questions/18193792/where-is-rubys-string-literal-juxtaposition-feature-officially-documented http://stackoverflow.com/questions/12120944/how-does-concatenation-of-two-string-literals-work – Bob Kuhar Oct 01 '15 at 00:04