method
marshal_load
Register or
log in
to add new notes.
tadman -
May 19, 2009
0 thanks
Method functions like Hash#merge!
This method functions a lot like Hash#merge! only with a different name.
f = OpenStruct.new # => #<OpenStruct> f.marshal_load({:foo => 'bar'}) # => #<OpenStruct foo="bar"> f.foo # => "bar"
tadman -
May 20, 2009
0 thanks
Symbol Keys Only
While OpenStruct#new is rather indifferent to the kind of keys submitted, marshal_load requires Symbol keys only. Use of a string can cause difficulty.
To fix:
marshal_load(hash.inject({ }) { |h, (k,v)| h[k.to_sym] = v; h })
As a note, Rails has the Hash#symbolize_keys method that can be used in place.