On 9/18/07, Summercool <Summercoolness / gmail.com> wrote:

> i think the line
>
> a = "different"
>
> means a is now set to a pointer to the String object with content
> "different".
> or that "a is now a reference to the String object."

Yes!


> and b is still a reference to the Array object.  so that's why a and b
> print out different things.  they point to different objects.

Yes!

> i think:
>
> whenever in Ruby, Python, and Java,
>
> a is never an object.  a is always a "reference to an object"...  this
> will solve a lot of puzzles when we don't understand some code
> behaviors.

Yes, Yes, Yes!

> when a writing or a book reads "a is a Hash object; a is an Array
> object; or a is an Animal object" it is just a short form to say that
> "a is a reference to that object."
>
> b = a means "whatever a is referencing to, now b is referencing it
> too".
>
> so that's why  a[1] = "foobar"  will change what b will display, but
> a = "foobar" will not change what b will display.  (because a[1] =
> "foobar" says "what is  a  referencing?  go there and change its
> content that has the index 1"  and when b goes there to see it, it is
> also changed.)

Exactly!

As a long-time observer and contributor to this forum, I've got to say
that this subtle distinction between variables and objects has tripped
up more than it's fair share of newbies.  It might well be the #1
stumbling block for many ruby learners. Not to mention other similar
languages.

-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/