On Fri, Nov 13, 2009 at 2:30 PM, PIKI <avid.piks / gmail.com> wrote:

> But I am struggling to copy an array.
>
> I am using
>
> a = []
> b = a
>
> /* some manipulation here on "b" and "a" I havent touched at all*/
>
> Here when i am trying to see contents of "a" and "b" both have same
> contents
>
> But my expectation was they shud be different
>
> Instead of b = a, I also tried b.replace(a)
>
> but no luck here also.
>
> Please help me on how to make a duplicate("b") of "a" so that changes
> done on b will not affect a

irb(main):001:0> a = [1,2,3,4]
=> [1, 2, 3, 4]
irb(main):002:0> b = a.dup
=> [1, 2, 3, 4]
irb(main):003:0> b[0] = 55
=> 55
irb(main):004:0> a
=> [1, 2, 3, 4]
irb(main):005:0> b
=> [55, 2, 3, 4]

Hope this helps,

Jesus.