Read more

How to convert an OpenStruct to a Hash

Arne Hartherz
September 18, 2012Software engineer at makandra GmbH

Simply use OpenStruct#to_h to receive an OpenStruct's hash representation.
In older Rubies you need OpenStruct#marshal_dump instead.

Ruby 2.0+

>> OpenStruct.new(foo: 23, bar: 42).to_h
=> { :foo => 23, :bar => 42 }

Ruby 1.9.3 and 1.8.7

>> OpenStruct.new(:foo => 23, :bar => 42).marshal_dump
=> { :foo => 23, :bar => 42 }
Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

Both approaches will return the underlying hash table (a clone of it in Ruby 2.0+).

Posted by Arne Hartherz to makandra dev (2012-09-18 13:50)