Module: Hashtostruct::HashToStruct

Defined in:
lib/hashtostruct.rb

Instance Method Summary collapse

Instance Method Details

#to_objObject



82
83
84
# File 'lib/hashtostruct.rb', line 82

def to_obj
  to_struct
end

#to_structObject

Takes a Hash and converts it into a Struct with each key as a property each value converted into a native object if possible.



75
76
77
78
79
80
# File 'lib/hashtostruct.rb', line 75

def to_struct
  hash = self
  Struct.new(*(hash.inject([]) { |res,val| res << val[0].to_sym })).new(*(
    hash.inject([]) { |res,val| res << val[1].to_obj}
  ))
end