From: Justin Collins Date: 2010-08-05T23:57:20+09:00 Subject: Re: String concatenation in ruby On 08/05/2010 06:47 AM, Prachi Tripathi wrote: > Hi, > > This seems to be a basic question but i would like to know why. > > String concatenation in rails can be done in the following three ways: > > 1. a+b+c > 2. a< 3. "#{a}#{b}#{c}" > > where a b c are three variables > > Which is the good way to go about concatenation and why? > > a+b+c is not good because it creates temp string objects i guess > > But i would really like to know of the three which is better and why. Is > there any performance difference in using them > > Thanks in advance > As you mention, using + will create strings which then need to be garbage collected. Using << will modify the left-most variable, which you may not always desire. The other downside to using + or << is that they will raise an error if any of your variables are not strings. If you use interpolation, to_s will be called on the variables which may save your program from crashing. Interpolation also appears to be the fastest method: http://rubybenchmark.com/reports/6 -Justin