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 ? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 ? On Mar 25, 11:34 am, Adrien Coquio <adrien.coq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 ?-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
On Mar 25, 2010, at 10:34 AM, Adrien Coquio wrote:> 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 ?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 Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 25 March 2010 14:47, Adrien Coquio <adrien.coquio-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Thank you very much for your explanations :) adrien 2010/3/25 Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>> On 25 March 2010 14:47, Adrien Coquio <adrien.coquio-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- COQUIO Adrien adrien.coquio-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.