Skip to content

Instantly share code, notes, and snippets.

@virullius
Created July 27, 2015 15:56
Show Gist options
  • Save virullius/6f3588a39f23645db0f5 to your computer and use it in GitHub Desktop.
Save virullius/6f3588a39f23645db0f5 to your computer and use it in GitHub Desktop.
Dynamic constructor example with hash args
class Thing
attr_accessor :color, :size
def initialize args = {}
args.each_pair do |k,v|
if self.respond_to?(k)
self.send("#{k}=", v)
else
raise ArgumentError.new "unknown property: #{k}"
end
end
end
end
thing = Thing.new(color: 'blue')
p thing
thing.size = 'medium'
p thing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment