On 9/29/07, Morton Goldberg <m_goldberg / ameritech.net> wrote:
> On Sep 29, 2007, at 1:16 PM, SpringFlowers AutumnMoon wrote:

Please forget anything and everything you ever learned about pointers
and references with respect to Ruby. It simply doesn't apply. This is a
good thing.

>> when we say

>> a = Dog.new("lulu")

>> Now a is not really a pointer, because we don't need to dereference
>> it to use it, like

No, it isn't a pointer. Period. I'm going to introduce a new concept to
you in a moment.

>> (*a).color = "red"
>> a->color = "red"
>>
>> When we use a.color, it is like a reference in C++ implicitly
>> dereference it and use its attributes.
>>
>> But then a is not really a reference (like C++), because we can say
>>
>> a = nil  or   a = Dog.new("woofy")
>>
>> and now a points to some where else.  With reference, once a
>> reference is set, it cannot point to some where else (in C++).

No, that's not true. You can, with some difficulty, rereference.

>> So it is kind of a mixture of pointer and reference?

Nope.

>> Or, we can think of it as a pointer, and then think of "." as the
>> "->" in C++.

No, you can't.

>> In that case, we can say that a is a pointer and not a reference.

No, you can't.

>> And it seems the same way in Java, Python, and PHP5.

I can't speak toward Python, but Java and PHP treat variables
differently than Ruby.

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.

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.

A Ruby variable is nothing like a C++ variable. Never has been, never
will be.

(Don't think that Symbols are special, either. They're not. Ruby just
keeps a list of all sticky notes that were ever created so you can,
within a certain scope and context, see if the objects you want know
anything about those particular sticky notes. It's sort-of a master
index, that way. But it's not magic. It's how you use them that's
magic.)

> [*] 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.

No, he's not. Bjarne is clearly not a mere mortal. Or from the upper
planes.

-austin
-- 
Austin Ziegler * halostatue / gmail.com * http://www.halostatue.ca/
               * austin / halostatue.ca * http://www.halostatue.ca/feed/
               * austin / zieglers.ca