rubyonrailsin

A Ruby and Rails talk

Thursday, March 25, 2010


Re: [Rails] Re: Dup a Constant

by rubyonrailsin 0 comments

Tag


Share this post:
Design Float
StumbleUpon
Reddit

Thank you very much for your explanations :)

adrien

2010/3/25 Colin Law <clanlaw@googlemail.com>
On 25 March 2010 14:47, Adrien Coquio <adrien.coquio@gmail.com> wrote:
> 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 ?
>

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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.




--
COQUIO Adrien
adrien.coquio@gmail.com

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment

Subscribe feeds via e-mail

Blog Archive