From: "Mills Thomas (app1tam)" Date: 2002-10-24T04:05:49+09:00 Subject: Re: Things That Newcomers to Ruby Should Know (10/16/02) Actually, the way you describe '+=' makes sense to me. It is what I would have expected. If I'm refering to one object in two different ways (that is, two different variables), why not have side-effects? That doesn't seem so very odd. Granted I've been doing OO stuff for about 17 years, but still, it's not like it took that long to grok objects and references. Drew > -----Original Message----- > From: William Djaja Tjokroaminata [mailto:billtj@z.glue.umd.edu] > Sent: Wednesday, October 23, 2002 12:00 PM > To: ruby-talk@ruby-lang.org > Subject: Re: Things That Newcomers to Ruby Should Know (10/16/02) > > > Hi, > > As already been pointed out, this is because in Ruby when a > variable is assigned to another variable, the new variable > simply refers to the same object that the old variable refers to: > > a = AnObject.new > b = a # b really refers to the same, identical AnObject as a > > Therefore, if '+=' means 'append()', then what you will have is > > b += anotherObj # a will also be a = a.append(anotherObj) > > The only safe/natural way of having '+=' to mean 'append()' > is by changing the semantic in Ruby by having the variable > assignment operator to invoke some copy method: > > # Not a Ruby code > a = AnObject.new > b = a # b = a.copy() > b += anotherObj # b = b.append(anotherObj), a unchanged > > But Ruby does not work that way; you have to use C++ if that > is really the intention. > > Regards, > > Bill