如果有修改method_missing,那最好也重写respond_to_missing?
但手动检查method时,还是应该使用respond_to?


调用method来获取ghost method时,如果respond_to_missing? 返回true,可返回method_missing的代理。

当然,respond_to_missing?和method_missing的判断逻辑应该一致,否则返回了也不能对它call得如意

class Plain
  def method_missing name, *args, &blk
    if name == :a
      puts 'aaa'
    else
      super
    end
  end

  def respond_to? name, *args
    name == :b || super
  end

  def respond_to_missing? name, *args
    name == :c || super
  end

end

pn = Plain.new

puts pn.respond_to? :a
puts pn.respond_to? :b
puts pn.respond_to? :c

%w[a b c].each do |name|
  begin
    m = pn.method name
    m.call
  rescue Exception => e
    puts e
  end
end


以上输出:

false
true
true
undefined method `a' for class `Plain'
undefined method `b' for class `Plain'
undefined method `c' for #