Ary Borenszweig

Ary Borenszweig

String concatenation in ruby

1 min
Feb 22 2010
coding
1 min
Feb 22 2010

I just found out the difference between += and << when used for a string.

irb(main):001:0> str = 'chan'
=> "chan"
irb(main):002:0> str.object_id
=> 69952758899780
irb(main):003:0> str += 'ged'
=> "changed"
irb(main):004:0> str.object_id
=> 69952758848800
irb(main):005:0> str << ', now not'
=> "changed, now not"
irb(main):006:0> str.object_id
=> 69952758848800

So I think that basically you should never use += for strings, unless you really want to hurt the garbage collector.