1 Mar 2005 01:05
Re: aliasing variables
Eliah Hecht <eliahhecht <at> gmail.com>
2005-03-01 00:05:29 GMT
2005-03-01 00:05:29 GMT
I mean, can't I just tell ruby that b refers to a, instead of referring to a's value? On Mon, 28 Feb 2005 16:04:20 -0800, Eliah Hecht <eliahhecht <at> gmail.com> wrote: > I know why it doesn't work, I just want to know how to make it work > (if it's doable without making a wrapper object). > > > On Tue, 1 Mar 2005 08:57:41 +0900, Hal Fulton <hal9000 <at> hypermetrics.com> wrote: > > Eliah Hecht wrote: > > > Is there a way to do something like > > > a = 1 > > > alias(b,a) > > > a += 1 > > > where the desired effect is that now b == 2? > > > > > > > It's impossible in the general case, because > > a+=1 literally means a=a+1 (in other words, > > it creates a new object and assigns that to > > a). > > > > Fixnums are not mutable at all. But if an > > object is mutable, and an operator modifies > > the object rather than creating a new one, > > the behavior you want will happen: > > > > a = "cat" > > b = a > > a << "hode" > > puts b # cathode > > > > However, assignment never keeps the same > > object: > > > > a = "cat" > > b = a > > a += "hode" > > puts b # cat > > > > In short: Assignment works on variables, not > > objects; but methods are called on objects, > > not variables. > > > > Make sense? > > > > > > Hal > > > > >
RSS Feed