我在 Windows 上使用 Ruby 1.9.3。所以我想通过Kernel
类添加在控制台中可用的方法。启动控制台。
module Foo
def bar
puts "Method is in scope!!!"
end
end
将其添加到Kernel
(这是Object
类的一部分)之后
irb(main):008:0> Kernel.send(:include, Foo)
=> Kernel
irb(main):009:0> bar
"NameError: undefined local variable or method `bar' for main:Object"
# did not work, we need to re-include Kernel in Object class
irb(main):010:0> Object.send(:include, Kernel)
=> Object
irb(main):011:0> bar
Method is in scope!!!
=> nil
irb(main):012:0>
这应该只适用于Kernel.send(:include, Foo)
还是我错了?我错过了什么吗?