Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to copy array in ruby

0 views
Skip to first unread message

PIKI

unread,
Nov 13, 2009, 8:29:31 AM11/13/09
to
HI

I am a novice in Ruby programming so this question might look trivial
to lot of people.

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

Thanks
Pikender Sharma

Jesús Gabriel y Galán

unread,
Nov 13, 2009, 8:34:29 AM11/13/09
to
On Fri, Nov 13, 2009 at 2:30 PM, PIKI <avid...@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.

PIKI

unread,
Nov 14, 2009, 5:24:02 AM11/14/09
to
On Nov 13, 6:34 pm, Jesús Gabriel y Galán <jgabrielyga...@gmail.com>
wrote:
> Jesus.- Hide quoted text -
>
> - Show quoted text -

Hi Jesús Gabriel y Galán

Thanks for the reply and help

I tried ur solution but it is also not working :(

I am unable to figure out the trick

Thanks
Pikender Sharma

lith

unread,
Nov 14, 2009, 6:25:22 AM11/14/09
to
> I tried ur solution but it is also not working :(
>
> I am unable to figure out the trick

In order to help us to figure out what is the problem, you might want
to post a code snippet demonstrating the defect and explain what how
you would expect ruby to behave.

Array#dup creates a copy of the array, not of the elements in the
array, aka a shallow copy.

Ralph Shnelvar

unread,
Nov 14, 2009, 7:20:00 AM11/14/09
to
l> Array#dup creates a copy of the array, not of the elements in the
l> array, aka a shallow copy.

Is there a way to make a deep copy? That is ... in the code below,
could b.dup be replaced with something like b.deepdup?


irb(main):019:0> a1 = [1,2,3,4]


=> [1, 2, 3, 4]

irb(main):020:0> b = [a1,5]
=> [[1, 2, 3, 4], 5]
irb(main):021:0> c1 = b
=> [[1, 2, 3, 4], 5]
irb(main):022:0> c2 = b.dup
=> [[1, 2, 3, 4], 5]
irb(main):023:0> a1[0] = "hi"
=> "hi"
irb(main):024:0> a1
=> ["hi", 2, 3, 4]
irb(main):025:0> c1
=> [["hi", 2, 3, 4], 5]
irb(main):026:0> c2
# because of the shallow copy, c2[0][0] is "hi" instead of a naively
# assumed 1.
=> [["hi", 2, 3, 4], 5]
irb(main):027:0> c1[1] = "bye"
=> "bye"
irb(main):028:0> c1
=> [["hi", 2, 3, 4], "bye"]
irb(main):029:0> c2
# The shallow copy gives the expected result for c2[1], below.
=> [["hi", 2, 3, 4], 5]

Derrick Fiedler

unread,
Nov 14, 2009, 1:21:41 PM11/14/09
to
you can make it via this method "clone" b = a.clone
0 new messages