Skip to content

Instantly share code, notes, and snippets.

@indirect
Created April 28, 2010 17:54
Show Gist options
  • Save indirect/382451 to your computer and use it in GitHub Desktop.
Save indirect/382451 to your computer and use it in GitHub Desktop.
structs initialized from a hash
class Foo < Struct.new(:foo, :bar)
def self.from_hash(hash)
new(*new.members.map{|a| hash[a] }) if hash
end
end
Foo.from_hash("bar" => "bagels", "foo" => "cream cheese")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment