Skip to content

Instantly share code, notes, and snippets.

@mikecmpbll
Created September 16, 2014 13:42
Show Gist options
  • Save mikecmpbll/64200a410eb553df805e to your computer and use it in GitHub Desktop.
Save mikecmpbll/64200a410eb553df805e to your computer and use it in GitHub Desktop.
class AbstractClass
def initialize(attributes)
attributes.each do |name, value|
unless self.respond_to?("#{name}=")
self.class.send(:attr_accessor, name)
end
self.send("#{name}=", value)
end
end
end
my_class = Class.new(AbstractClass)
=> #<Class:0x007fe185762960>
c = my_class.new(a: 1, b: 2)
=> #<#<Class:0x007fe185762960>:0x007fe1857698f0 @a=1, @b=2>
c2 = my_class.new(d: 1, e: 2)
=> #<#<Class:0x007fe185762960>:0x007fe185791e90 @d=1, @e=2>
c2.a
=> nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment