b.class.superclass <= "Foo"
, , , self Bar, b.
b.class <= always going to be Bar
self.class <= always going to be Bar if you are invoking Bar.
, . , ... , , -
class Bar
def initialize
puts 'In BAR class'
end
def foo
p self.class.name
end
end
Bar.class_eval do
def brand_new_method
p "Still in Bar, but this is dynamically added"
end
end
Perhaps you are talking about dynamically adding methods to the classes above in the inheritance chain ... to "Foo" in your example ... based on some conditional event in the Bar instance. If so, then why not use a single module to define your inherited methods:
module Foo
def foo
p self.class
end
end
and then use module_eval just like class_eval?
source
share