Jes' Codex

jes5199 of blog
Posts I Like

In real code, we found a very, very slow loop. Changing two characters:

+= is about 100 seconds.
<< is around 1/3 of a second.

Using += on a string creates a copy and then discards the original, while << allows the ruby interpreter to intelligently resize the buffer.

The only gotcha is that << will mutate your string object, which means that it’s not safe to use on a method’s parameters. #dup those strings, if you need to append to them.