First, think about what you are doing include. This makes the method an instance of the module included in the instance methods for class inclusion. that is, besides the fact that your working example uses an anonymous module, it is equivalent:
module M1
def lab
puts 'm'
end
end
class Foo
include M1
def lab
super
puts 'c'
end
end
, class_eval. . , , class_eval. , MyModule = Module.new { class_eval "def lab; puts 'm' end" }
module MyModule
def lab
puts 'm'
end
end
, , .
instance_eval, ( ), MyMod2 = Module.new { instance_eval "def lab; puts 'm' end" }
module MyMod2
def MyMod2.lab
puts 'm'
end
end
. , MyMod2.lab, include.
: , instance_eval vs. class_eval, Ruby . .