From: Morton Goldberg Date: 2007-09-30T08:19:12+09:00 Subject: Re: a = Dog.new # a is not a pointer and not a reference? On Sep 29, 2007, at 4:13 PM, Austin Ziegler wrote: > On 9/29/07, Morton Goldberg wrote: > A variable in Ruby isn't like Java, PHP, C++, C, or even Pascal. > >> I think it best to think of a Ruby variable as holding a reference to >> an object. Ruby's reference semantics are different from C++'s, but >> IMO more mainstream. C++'s reference semantics are peculiar, to say >> the least, and perhaps even unique [*]. Ruby's variable semantics are >> simple and clean when compared to C++, so I recommend forgetting >> about making such comparisons. My use of "holding" may be unfortunate. I only meant it conceptually. I was trying to make things simple for the OP. > No, it's best to forget the concept of a Ruby variable as a > shoebox. It > doesn't *hold* anything. > > In C/C++, a variable is a shoebox. This shoebox has a physical > location > and dimension (the address in memory). One shoebox can hold the > location > of another shoebox. That's a pointer or a reference (under the covers, > they are the same in C++). > > A variable in Ruby is a sticky note (like a Post-It). You can put that > sticky note on an object, or you can put multiple sticky notes on an > object. But if you move the sticky note, you're changing the object to > which it references. Since they're labels, the sticky notes don't > contain anything -- they just name the object that they're attached > to. > Since they don't contain anything, no other sticky note can point to > another sticky note. By "sticky notes" you seem to be talking about bindings, like a Lisp variable makes. That is how I think of Ruby variables, myself, but I didn't think bringing bindings up would help the OP. I didn't think of the analogy with sticky notes. I like that -- I'll have to remember it. > A Ruby variable is nothing like a C++ variable. Never has been, never > will be. We are in complete agreement. That was the point I trying to make. Did I phrase it so obscurely that you took me to be saying the opposite? >> [*] Betrand Meyer of Eiffel fame has often made fun of C++'s >> reference >> semantics. He has claimed they are beyond the understanding of mere >> mortals. He is joking, of course. That should be Bertrand not Betrand. Pardon my typo. > No, he's not. Bjarne is clearly not a mere mortal. Or from the > upper planes. Are you implying Bjarne Stroustrup is some kind of demon? He always struck me as a very nice person. Regards, Morton