On Fri, Nov 12, 2010 at 1:09 PM, Robert Dober <robert.dober / gmail.com> wrote:

> Are you sure Xavier?
> I am afraid that in
>
> def x b
> b = ...
> you brake the link as shown in the diagram (somehow as having a local
> variable shadowing the param)
>
> because if you made
>
> def x b
> b << "hello"
>
> the diagram seems correct, or is it I who misses something here?

Not sure I follow.

Variables can't store objects, that's guaranteed by the Ruby spec,
they only store references.

There, b holds a reference to a string. Strings are mutable in Ruby,
and << appends in place. The b variable holds a reference to the same
object stored in the variable of the caller. This is no different than
passing a user instance and calling user.name= inside the method.

Let's suppose for a moment that the object_id serves as reference, as
if it was a pointer. Let's imagine "" has object id 2158937800. The
diagram is:

    a -> 2158937800 -> ""
    b -> 2158937800 -> ""

Note that a and b store the same reference value, but they are separate.

If we assign a new string "b" to b inside the method with object id
2158934000 the diagram becomes

    a -> 2158937800 -> ""
    b -> 2158934000 -> "b"

because a and b store different values.