# Dup a Constant

**URL:** <https://www.ruby-forum.com/t/dup-a-constant/185747>\
**Category:** Rails\
**Created:** [March 25, 2010, 3:35pm UTC](https://www.ruby-forum.com/t/dup-a-constant/185747 "2010-03-25T15:35:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![korn900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5daacb/32.png) [@korn900](https://www.ruby-forum.com/u/korn900)\
**Post date:** [March 25, 2010, 3:35pm UTC](https://www.ruby-forum.com/t/dup-a-constant/185747/1 "2010-03-25T15:35:20Z")

</div>

I have a constant in my helper :  
MYCONSTANT = { :opt =\> { :title =\> “abcdef” } }

In my view :  
\<%  
joke = PeopleHelpers::MYCONSTANT.dup  
joke[:opt][:title] = nil  
#if i debug there, PeopleHelpers::MYCONSTANT = {:opt =\> {:title =\>  
nil}}  
%\>

I can’t understand this behaviour and how fix it ?  
Is it a bug ?

---

<div class="post-metadata">

**Author:** ![korn900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5daacb/32.png) [@korn900](https://www.ruby-forum.com/u/korn900)\
**Post date:** [March 25, 2010, 3:48pm UTC](https://www.ruby-forum.com/t/dup-a-constant/185747/2 "2010-03-25T15:48:48Z")

</div>

In fact the previous example doesn’t change the value of  
PeopleHelpers::MYCONSTANT

but if I do :  
\<%  
joke = PeopleHelpers::MYCONSTANT.dup  
joke[:opt][:title].sub!(‘a’,‘b’)  
#if i debug there, PeopleHelpers::MYCONSTANT = {:opt =\> {:title =\>  
bbcdef}}  
%\>

the value changed… why ?

---

<div class="post-metadata">

**Author:** ![korn900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5daacb/32.png) [@korn900](https://www.ruby-forum.com/u/korn900)\
**Post date:** [March 25, 2010, 4:14pm UTC](https://www.ruby-forum.com/t/dup-a-constant/185747/3 "2010-03-25T16:14:34Z")

</div>

On 25 March 2010 14:47, Adrien C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> the value changed… why ?

My understanding is that dup makes a copy of the hash contents, but  
since the contents are themselves references rather than simple  
objects you end up with a new hash containing references to the same  
objects, which can then be modified as you have noted (even though  
they are notionally constant).

Colin

---

<div class="post-metadata">

**Author:** ![korn900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5daacb/32.png) [@korn900](https://www.ruby-forum.com/u/korn900)\
**Post date:** [March 25, 2010, 4:03pm UTC](https://www.ruby-forum.com/t/dup-a-constant/185747/4 "2010-03-25T16:03:26Z")

</div>

On Mar 25, 2010, at 10:34 AM, Adrien C. wrote:

> I can’t understand this behaviour and how fix it ?  
> Is it a bug ?

No, #dup and #clone typically make shallow copies.

irb\> orig = { :opt =\> { :title =\> ‘hello’ } }  
=\> {:opt=\>{:title=\>“hello”}}  
irb\> dup = orig.dup  
=\> {:opt=\>{:title=\>“hello”}}  
irb\> dup[:opt][:title]  
=\> “hello”  
irb\> dup[:opt][:title] = nil  
=\> nil  
irb\> dup  
=\> {:opt=\>{:title=\>nil}}  
irb\> orig  
=\> {:opt=\>{:title=\>nil}}

irb\> orig = { :opt =\> { :title =\> ‘hello’ } }  
=\> {:opt=\>{:title=\>“hello”}}  
irb\> clone = orig.clone  
=\> {:opt=\>{:title=\>“hello”}}  
irb\> clone[:opt][:title]  
=\> “hello”  
irb\> clone[:opt][:title] = nil  
=\> nil  
irb\> clone  
=\> {:opt=\>{:title=\>nil}}  
irb\> orig  
=\> {:opt=\>{:title=\>nil}}

If you want a “deep” copy, usually you Marshal and un-Marshal:

irb\> orig = { :opt =\> { :title =\> ‘hello’ } }  
=\> {:opt=\>{:title=\>“hello”}}  
irb\> marshaled = Marshal.load(Marshal.dump(orig))  
=\> {:opt=\>{:title=\>“hello”}}  
irb\> marshaled[:opt][:title]  
=\> “hello”  
irb\> marshaled[:opt][:title] = nil  
=\> nil  
irb\> marshaled  
=\> {:opt=\>{:title=\>nil}}  
irb\> orig  
=\> {:opt=\>{:title=\>“hello”}}

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![korn900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5daacb/32.png) [@korn900](https://www.ruby-forum.com/u/korn900)\
**Post date:** [March 25, 2010, 8:38pm UTC](https://www.ruby-forum.com/t/dup-a-constant/185747/5 "2010-03-25T20:38:06Z")

</div>

Thank you very much for your explanations 🙂

adrien

2010/3/25 Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > %\>  
> > Colin

–  
COQUIO Adrien  
removed\_email\_address@domain.invalid
