Convert a hash into a struct

ruby

Solution

If it doesn't specifically have to be a `Struct` and instead can be an `OpenStruct`:

pry(main)> require 'ostruct'
pry(main)> s = OpenStruct.new(h)
=> #<OpenStruct a=1, b=2>
pry(main)> puts s.a, s.b
1
2

Problem

How can I convert a hash into a struct in ruby? Given this: ``` h = { :a => 1, :b => 2 } ``` I want a struct such that: ``` s.a == 1 s.b == 2 ```

Original source

Related problems