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
[email protected]
--
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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.