2013-10-31 83 views

回答

3
class MessageResponder 
    def method_missing(method, *args, &block) 
    "You called #{method}(#{args.map(&:inspect).join(', ')})#{' with block' if block}" 
    end 
end 

responder = MessageResponder.new 
responder.foo(3, 7) 
# => You called foo(3, 7) 

如果消息不对应于任何的类的方法,则称为method_missing该方法被调用。您可以在示例中看到它收到的内容。如果你覆盖它,你可以回复任何消息。

1

没有匹配方法的消息发送到method_missing。不管你喜欢什么,你都可以实现它。

相关问题