在本文的启发下,我正在使用Ruby方法调用层次结构并注意到一些奇怪的东西.
鉴于:
class B
def foo
"- Instance method defined by B"
end
def method_missing(method)
puts "- method_missing (#{method}) on b. Redirecting to b.foo\n"
foo
end
end
b = B.new
def b.foo
"- Method defined directly on an instance of B\n" + super
end
def b.method_missing(method)
"- method_missing (#{method}) on b. Calling super\n" + super
end
puts "Calling 'bar' on b of type #{b.class}:"
puts b.bar
Run Code Online (Sandbox Code Playgroud)
运行它给出:
Calling 'bar' on b of type B:
- method_missing (bar) on …Run Code Online (Sandbox Code Playgroud) ruby ×1