Martin DeMello
8/19/2008 11:27:00 PM
On Tue, Aug 19, 2008 at 4:16 PM, Jonah Bloch-Johnson
<jsb2142@columbia.edu> wrote:
>
> def haves
> x = [1]
> y = []
> y << x
> x = [2] # (compare this)
> y
> end
>
> Why does meddling with the innards of the "x" have retroactive
> repercussions, whereas redefining it outright does not?
When you say 'x = [2]' you aren't redefining the object x, you are
taking the variable 'x' and binding it to a new object. When you say y
<< x, you are inserting the object that x was pointing to at that
moment into y, not the variable x. Think of a variable as an alias for
an object - what is actually passed around are the objects themselves,
but you can refer to them by any of their current aliases.
martin