Skip to content

Instantly share code, notes, and snippets.

@liwh
Created September 10, 2010 09:33
Show Gist options
  • Save liwh/573376 to your computer and use it in GitHub Desktop.
Save liwh/573376 to your computer and use it in GitHub Desktop.
//Hash to Struct
class Hash
def to_struct(struct_name)
Struct.new(struct_name,*keys).new(*values)
end
end
if $0 == __FILE__
h = {:name=>"Dan","age"=>33,"rank"=>"SrA","grade"=>"E4"}
s = h.to_struct("Foo")
puts "name: " + s.name
puts "age: " + s.age.to_s
puts "rank: " + s.rank
puts "grade: " + s.grade
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment