Created
April 12, 2013 14:55
-
-
Save apeiros/5372621 to your computer and use it in GitHub Desktop.
This file contains 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
def Object.create(attrs) | |
obj = new | |
obj.singleton_class.send(:attr_accessor, *attrs.keys) | |
attrs.each do |name,value| obj.instance_variable_set(name.to_s.sub(/\A@?/, '@'), value) end | |
obj | |
end | |
o = Object.create(foo: 12, bar: "hi") # => #<Object:0x007fc53a0f7bc8 @bar="hi", @foo=12> | |
o.bar # => "hi" | |
o.bar = "foo" # => "foo" | |
o # => #<Object:0x007fc53a0f7bc8 @bar="foo", @foo=12> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment