是的。如果一个对象不回复短信,红宝石将发送method_missing
消息的消息选择和参数来接收:
class X
def method_missing(selector, *args, &blk)
puts "The message was #{selector.inspect}."
puts "The arguments were #{args.map(&:inspect).join(', ')}."
puts "And there was #{blk ? 'a' : 'no'} block."
super
end
end
x = X.new
x.some_method('some args', :some_other_args, 42)
# The message was :some_method.
# The arguments were "some args", :some_other_args, 42.
# And there was no block.
# NoMethodError: undefined method `some_method'
x.some_other_method do end
# The message was :some_other_method.
# The arguments were .
# And there was a block.
# NoMethodError: undefined method `some_other_method'
请注意,如果你定义method_missing
,你也应该定义respond_to_missing?
相应。否则,你得到奇怪的行为是这样的:
x.respond_to?(:foo) # => false
x.foo # Works. Huh?
在这种特殊情况下,我们处理所有的消息,因此我们可以简单的如下定义它:
class X; def respond_to_missing?(*) true end end
x.respond_to?(:foo) # => true
-1这东西完全无关。再次阅读问题。 – delnan 2011-03-11 13:46:32
啊,我的坏..误读了这个问题.. :( – RameshVel 2011-03-11 14:02:42