In Ruby, variables reference objects rather than contain values.
Therefore, when you write b = a, b references the same object as a does.
Therefore, when you modify b, you modify a.
See the obejct_ids in your code:

On Sun, Aug 17, 2008 at 4:22 PM, Marcio Braga <> wrote:
> Just to complement, as a contrast, the code below works as expected, or
> in other words, the content of matrix "a" is not changed when you change
> the content of matrix "b".
>
> b=[]
> a=[1]
> b[0]=a[0] # explicitly set a single element and not the entire
> matrix
> b[0]=2*b[0]
> p a
>
> Marcio
>
>

Here you create explicitely two objects therefore : no problem whereas
on the first snippet, you only have one.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!