Skip to content

Instantly share code, notes, and snippets.

@vzvu3k6k
Forked from hiyosi/gist:2b04e1e4907be17207c6
Last active August 29, 2015 14:07
Show Gist options
  • Save vzvu3k6k/ce32d270ca5cc8cd7077 to your computer and use it in GitHub Desktop.
Save vzvu3k6k/ce32d270ca5cc8cd7077 to your computer and use it in GitHub Desktop.
class Hoge
def self.hoge
obj = method(__method__).receiver.new
attributes.each do |key|
obj.send("#{key}=", key.to_s + "!!!")
end
p obj
end
def self.attributes
end
end
Foo = Class.new(Hoge) do
attr_accessor :name, :email
def self.attributes
[:name, :email]
end
end
Bar = Class.new(Hoge) do
attr_accessor :id, :pass
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!!!">
@vzvu3k6k
Copy link
Author

vzvu3k6k commented Oct 9, 2014

method(__method__).receiverHogeにならないのは意外。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment