From: recursive.madman@... Date: 2014-12-01T15:59:40+00:00 Subject: [ruby-core:66622] [ruby-trunk - misc #10560] confusion between x=x+y, x+=y, x.concat(y) and y.each{|z| x< 'foobar' y #=> 'bar' ``` As well as for integers: ``` x = y = 7 x += 3 x #=> 10 y #=> 7 ``` That is `x += y` is semantically identical to `x = x + y`. `concat` on the other hand **does** change the object itself (and is specific to arrays, not everything that has `+` defined): ``` x = y = [1,2,3] x.concat([4, 5, 6]) x #=> [1,2,3,4,5,6] y #=> [1,2,3,4,5,6] ``` ---------------------------------------- misc #10560: confusion between x=x+y, x+=y, x.concat(y) and y.each{|z| x< [], "a".."z" => "", }.each do |base, storage| base = base.to_a basej = base class_name = storage.class.to_s x.report(class_name+'#concat') do a = storage.dup basej = base.join if storage == "" rep.times { a.concat(basej) } end x.report(class_name+'#<<') do a = storage.dup basej = base.join if storage == "" rep.times { base.each { |e| a << e } } end x.report(class_name+'#+=') do a = storage.dup basej = base.join if storage == "" rep.times { a += basej } end x.report(class_name+'#dup.concat') do a = storage.dup basej = base.join if storage == "" rep.times { a = a.dup.concat(basej) } end end end and here are results on my machine: user system total real Array#concat 0.000000 0.000000 0.000000 ( 0.001422) Array#<< 0.020000 0.000000 0.020000 ( 0.014356) Array#+= 1.270000 0.230000 1.500000 ( 1.498558) Array#dup.concat 2.720000 0.190000 2.910000 ( 2.915701) String#concat 0.000000 0.000000 0.000000 ( 0.001072) String#<< 0.030000 0.000000 0.030000 ( 0.025828) String#+= 0.130000 0.010000 0.140000 ( 0.135143) String#dup.concat 0.210000 0.020000 0.230000 ( 0.227470) -- https://bugs.ruby-lang.org/