2013-10-31 79 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。不管你喜歡什麼,你都可以實現它。

相關問題