Reputation: 819
I have a OpenStruct
hash like this:
#<OpenStruct object1={
"param1"=>"2",
"param2"=>"1"
},
object2={
"param1"=>"2",
"param2"=>"1"
},
object3={
"param1"=>"2",
"param2"=>"1"
}...
How can I use each
on this?
Upvotes: 21
Views: 7036
Reputation: 53319
OpenStruct has a method called marshal_dump that returns the underlying hash structure:
your_open_struct.marshal_dump.each{ |k,v| puts "#{k} => #{v}" }
If you are using Ruby 2.0, you can use also to_h like so:
your_open_struct.to_h.each{ |k,v| puts "#{k} => #{v}" }
Unlike marshal_dump
, which returns the actual hash structure, to_h
returns a hash with all the keys converted to symbols for easier access.
Upvotes: 30