From: Robert Dober Date: 2007-08-24T20:48:54+09:00 Subject: Re: something I just found out, am sharing (:newbish) On 8/24/07, David A. Black wrote: > Hi -- > > On Fri, 24 Aug 2007, Robert Dober wrote: > > > On 8/24/07, David A. Black wrote: > >> Hi -- > > > >> > >> Don't worry -- there's no Academie Anglaise :-) > > I am not French, only my wife is;), but it is a funny remark. > >> > >>> But seriously there is just no need to pay the cost of the > >>> construction of another String / Array. > >>> Performance difference is already enormous for moderately long strings : > >> > >> It depends, though; sometimes you might want a copy, sometimes you > >> might definitely not. So the + vs. << decision may not be premature, > >> and the right decision may not even be the optimizing one :-) > > Hmm David when one does > > a += b > > the copy is thrown away and you have the same semantics (save some > > implicit calls to to_s) as for > > a << b > > They're not interchangeable, though: > > a = "Hello" > b = " there" > c = a > > a += b > puts a # Hello there > puts c # Hello > > a = "Hello" > c = a > a << b > puts a # Hello there > puts c # Hello there That is indeed an important - probably the most important - point to make, I believe that people are not always aware that a+=b is a = a + b. I have to admit that I assumed that OP wanted the a << b behavior, but maybe he did not, that was quite a bad performance of mine... Robert > > > David > > -- > * Books: > RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) > RUBY FOR RAILS (http://www.manning.com/black) > * Ruby/Rails training > & consulting: Ruby Power and Light, LLC (http://www.rubypal.com) > > -- I'm an atheist and that's it. I believe there's nothing we can know except that we should be kind to each other and do what we can for other people. -- Katharine Hepburn