method

marshal_load

v1_8_7_330 - Show latest stable - Class: OpenStruct
marshal_load(x)
public

No documentation available.

# File lib/ostruct.rb, line 65
  def marshal_load(x)
    @table = x
    @table.each_key{|key| new_ostruct_member(key)}
  end

2Notes

Method functions like Hash#merge!

tadman · May 19, 2009

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

tadman · May 20, 2009

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.