I’d like to define methods on initialize that are specific for the
created
object. However it seems that the few lines of code below is defining
them
somewhere else since they are ‘overwriting’ each other:
============
class Foo
def initialize(method_name, &block)
self.class.send(:define_method, method_name, &block)
end
end
aa = Foo.new(:whoareyou) { puts “I am aa” }
bb = Foo.new(:whoareyou) { puts “I am bb” }
However it seems that the few lines of code below is defining them
somewhere else since they are ‘overwriting’ each other:
============
class Foo
def initialize(method_name, &block)
self.class.send(:define_method, method_name, &block)
end
end
You’re absoultely right about the methods being defined “somewhere
else”, they’re being defined in the class Foo, which both instances
share. If you want to define specific and different implementations
for the same method name you need to use singleton/instance
inheritance. Like this:
class Foo
def initialize(method_name, &block)
c = class << self; self; end
c.send(:define_method, method_name, &block)
end
end
That way each instance gets a private Class that they don’t share.
Take the above with a grain of salt, I don’t have an interpreter on
this computer, so I can test it to see if it works… This blog
article might be interesting if you care for a deeper explanation: