From: Gavin Sinclair Date: 2002-10-23T21:35:34+09:00 Subject: Re: Things That Newcomers to Ruby Should Know (10/16/02) From: "Mauricio Fern�ndez" > On Wed, Oct 23, 2002 at 06:28:29PM +0900, Bulat Ziganshin wrote: > > Hello Ian, > > > > Tuesday, October 22, 2002, 7:30:19 AM, you wrote: > > > > IM> I understand that, but since 'a' already exists prior to assigning a + b > > IM> to it, why does it need to be recreated? Where strings are concerned, > > IM> I don't see why this would be any different to 'a << b'. > > > > main problem is that object pointed by 'a' mauy be also pointed by > > another variable an if you change this object directly your code wil > > have SIDE EFFECT. are you really need this? :) so '<<' and other > > methods changing object itself must be used very carefully, preferably > > for local variables and other "own" objects > > > > example of this side effect: > > > > def a(s) > > s << '!' > > s += '?' > > return s > > end > > > > x='1' > > y=a(x) > > p x,y > > Would it be possible for Ruby to know whether an object is referenced by > several variables so += does '<<' or '... = ... + ...' when appropriate? > > Perhaps a bit in each String indicating whether it is shared or > not would make it... and GC issues could be ignored at first. I don't think so. += is syntax sugar for you-know-what. Imagine trying to explain to a newbie "... but except in this case ..." Side-effects are a part of life in Ruby (and not only in Ruby). Programmers have to deal with them. They're not even a bad thing. Gavin