Created
January 20, 2013 14:03
-
-
Save carlzulauf/4578897 to your computer and use it in GitHub Desktop.
Hash initialized Struct
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HashStruct | |
def self.new(*args) | |
Struct.new(*args) do | |
def initialize(hash) | |
super( *self.class.members.map{|k| hash[k] } ) | |
end | |
end | |
end | |
end | |
# Usage: | |
# Person = HashStruct.new(:name, :age) | |
# person = Person.new(:age => 33, :name => "Dave") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment