method
marshal_load
v2_2_9 -
Show latest stable
- Class:
OpenStruct
marshal_load(x)public
Provides marshalling support for use by the Marshal library.
2Notes
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"
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.