Consider the following example:
module SomeModule
def some_method
puts "Some Method - #{self}"
end
end
class SomeClass
def self.metaclass
class << self; self; end
end
end
SomeClass.instance_eval {include SomeModule} # instance method
SomeClass.instance_eval {extend SomeModule} # singleton method (class
method)
SomeClass.metaclass.instance_eval {include SomeModule} # class method
Using instance_eval on SomeClass sets the "ghost class" to be the
current class, so using include shouldn't put the methods on "ghost
class" instead of SomeClass (SomeClass class methods)? In this case
using instance_eval or class_eval has the same result, depending only if
we're using include or extend.
AFAIK the "problem" has to do with value of self, because private
methods are invoked on self (default receiver), so using instance_eval
or class_eval won't matter as self will be SomeClass in both cases.
That's why calling include in instance_eval/class_eval on metaclass
(self is now the "ghost class") is the same as using extend when self is
SomeClass.
I'm using Ruby 1.8.6 and at least to me this seems kind of a strange
behaviour because using def or define_method will have different results
if we use instance_eval. Does Ruby 1.9 maintains this behaviour?
In my humble opinion would make more sense if instance_eval/class_eval
maintain the same behaviour on all methods and not the "special"
behaviour on private methods. Can someone enlight me?
···
--
Posted via http://www.ruby-forum.com/.