From: "Simon Kröger" Date: 2006-06-22T05:50:39+09:00 Subject: Re: The difference between s=s+”a” ans s<< ”a” Robert Dober wrote: > On 6/21/06, Alan Moen wrote: >> >> Hi, >> >> I wonder what is the difference between s=s+"a" ans s<< "a"? > > > s = s + "a" > (1) compute a new String object "s" + a > (2) set s as a reference to the newly created object > > s<<"a" > > (1) append "a" to the String object s refers to. > > The latter is much faster and should be prefered when possible (it will not > be possible if s is frozen e.g.) I think s = s + "a" # or s += "a" should be the norm as long as you do not know that using it will cause you problems (really only performance problems come to my mind) Have a look at this: -------------------------------------- def greet_matz greeting greeting << ' matz!' puts greeting end def greet_nobu greeting greeting << ' nobu!' puts greeting end hello = 'hello' greet_matz hello greet_nobu hello -------------------------------------- #=> hello matz! #=> hello matz! nobu! you will get what you expect if you use += cheers Simon