0

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!

Addison
  • 3,791
  • 3
  • 28
  • 48

2 Answers2

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.

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
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