# Recursive dup

**URL:** <https://www.ruby-forum.com/t/recursive-dup/136690>\
**Category:** Ruby\
**Created:** [May 5, 2008, 4:02pm UTC](https://www.ruby-forum.com/t/recursive-dup/136690 "2008-05-05T16:02:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [May 5, 2008, 4:02pm UTC](https://www.ruby-forum.com/t/recursive-dup/136690/1 "2008-05-05T16:02:32Z")

</div>

Hi, it seems that “dup” method is not recursive, so if we do:  
obj\_b = obj\_a.dup  
then all the attributes of obj\_b will be a exact copy of attributes of  
obj\_a, and not references.

But if an attribute of obj\_a is an object containing objects inside  
then those will be referenced and not copied. This is:

* * *

class A  
attr\_accessor :att  
end

class Obj  
attr\_accessor :kk  
end

obj = Obj.new  
obj.kk = “KAKA1”  
a=A.new  
a.att=obj

a  
=\> #\<A:0xb7bf6de8 @att=#\<Obj:0xb7c0dcf0 @kk=“KAKA1”\>\>

b=a.dup  
=\> #\<A:0xb7becd97 @att=#\<Obj:0xb7c0dcf0 @kk=“KAKA1”\>\>

b.a.kk=“KAKA2”

## a =\> #\<A:0xb7bf6de8 @att=#\<Obj:0xb7c0dcf0 @kk=“KAKA2”\>\>

So finally, an attribute of an attribute of “a” is been modified when  
modifying “b”.  
I know that I should personalize “dup” method for class A, but is not  
any other way? Does exist any automatic way of doing it?  
I expect some method “dup\_recursive” that looks for objects into  
attributes recursively and clones them instead of reference.

Is there something as it?

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [May 5, 2008, 4:07pm UTC](https://www.ruby-forum.com/t/recursive-dup/136690/2 "2008-05-05T16:07:52Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

IÃ±aki Baz C. wrote:

| I know that I should personalize “dup” method for class A, but is not  
| any other way? Does exist any automatic way of doing it?  
| I expect some method “dup\_recursive” that looks for objects into  
| attributes recursively and clones them instead of reference.

If your storage of objects can be enumerated, you could use .each,  
duplicate the objects you store, and re-store them.

That doesn’t seem terribly efficient, though, so proceed with caution  
(but it may get you started in the right direction :).

* * *

Phillip G.  
Twitter: [twitter.com/cynicalryan](http://twitter.com/cynicalryan)  
Blog: [http://justarubyist.blogspot.com](http://justarubyist.blogspot.com)

Make sure comments and code agree.  
~ - The Elements of Programming Style (Kernighan & Plaugher)  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (MingW32)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iEYEARECAAYFAkgfFI4ACgkQbtAgaoJTgL9g/wCghdYgjUs5o0cNsmxItj8yHOT9  
pKEAnAxds5jvXuulwEphsjRtCx1UFwCP  
=TiIJ  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [May 5, 2008, 5:38pm UTC](https://www.ruby-forum.com/t/recursive-dup/136690/3 "2008-05-05T17:38:40Z")

</div>

Hi –

On Mon, 5 May 2008, IÃ±aki Baz C. wrote:

> attr\_accessor :att
> 
> * * *
> 
> So finally, an attribute of an attribute of “a” is been modified when  
> modifying “b”.  
> I know that I should personalize “dup” method for class A, but is not  
> any other way? Does exist any automatic way of doing it?  
> I expect some method “dup\_recursive” that looks for objects into  
> attributes recursively and clones them instead of reference.
> 
> Is there something as it?

The most common idiom for a deep clone is:

new\_obj = Marshal.load(Marshal.dump(old\_obj))

David

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [May 5, 2008, 5:52pm UTC](https://www.ruby-forum.com/t/recursive-dup/136690/4 "2008-05-05T17:52:49Z")

</div>

2008/5/5, David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> The most common idiom for a deep clone is:
> 
> new\_obj = Marshal.load(Marshal.dump(old\_obj))

It’s simply GREAT !!!

Thanks a lot !
