Skip to content

Instantly share code, notes, and snippets.

@hiyosi
Created July 30, 2014 15:39
Show Gist options
  • Save hiyosi/2b04e1e4907be17207c6 to your computer and use it in GitHub Desktop.
Save hiyosi/2b04e1e4907be17207c6 to your computer and use it in GitHub Desktop.
class Hoge
def self.hoge
obj = Kernel.const_get(klass.capitalize).new
attributes.each do |key|
obj.send("#{key}=", key.to_s + "!!!")
end
p obj
end
def self.klass
end
def self.attributes
end
end
Foo = Class.new(Hoge) do
attr_accessor :name, :email
def self.klass
self.name
end
def self.attributes
[:name, :email]
end
end
Bar = Class.new(Hoge) do
attr_accessor :id, :pass
def self.klass
self.name
end
def self.attributes
[:id, :pass]
end
end
Foo.hoge
Bar.hoge
# % ruby sample.rb
# #<Foo:0x007f871190b858 @name="name!!!", @email="email!!!">
# #<Bar:0x007f871190b498 @id="id!!!", @pass="pass!!!">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment