Ftf 3k3 <ftf3k3 / gmail.com> writes:

> Hello folks, I have a question about Ruby 1.8.7.
>
> After doing:
>
>   a = b = 1
>   a += 1
>
> a returns 2 and b returns 1.
>
> But if I try:
>
>   a = b = []
>   a << 1
>
> both a and b returns [1]. Why?

Because a<<1 doesn't modify a. It sends the message << with the
argument 1 to the object referenced by a.  But it's always the same
object that's referenced by a, and of course, it's the same that is
referenced by b too.

In the case of a+=1, you are misled by the syntax (too much syntax
gives the cancer of the semicolon).  a+=1 is actually a = a+1, which
is sending the message + with the argument 1 to the obejct referenced
by a.  That object will behave according to its method, and will
return a new object.  This new object will now be referenced by a.


-- 
__Pascal Bourguignon__