Skip to content

Instantly share code, notes, and snippets.

@apeiros
Created April 12, 2013 14:55
Show Gist options
  • Save apeiros/5372621 to your computer and use it in GitHub Desktop.
Save apeiros/5372621 to your computer and use it in GitHub Desktop.
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