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!
Asked
Active
Viewed 78 times
2 Answers
5
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.

Community
- 1
- 1

Jonathon Reinhart
- 132,704
- 33
- 254
- 328
-
Hm, thats interesting. Is there a practical application to this? – Addison Apr 21 '14 at 22:16
0
I've never noticed this before, and this looks to be another form of concatenation like <<
and +
.

Daniël W. Crompton
- 3,448
- 25
- 26