Method: Hash#to_struct

Defined in:
lib/core/facets/hash/to_struct.rb

#to_struct(struct_name) ⇒ Object

A method to convert a Hash into a Struct.

h = {:name=>"Dan","age"=>33,"rank"=>"SrA","grade"=>"E4"}
s = h.to_struct("Foo")

TODO: Is this robust enough considerd hashes aren’t ordered?

CREDIT: Daniel Berger


12
13
14
# File 'lib/core/facets/hash/to_struct.rb', line 12

def to_struct(struct_name)
  Struct.new(struct_name,*keys).new(*values)
end