多个参数,我有以下的模型来实现method_missing
代码:为method_missing的
# class Thought
def self.method_missing(method_id, *arguments, &block)
if $CLIENT.respond_to?(method_id)
$CLIENT.send(method_id, *arguments, &block)
# Do some stuff with it
else
super
end
end
$CLIENT
是一个全局对象。请注意,对于类,这是method_missing
,而不是实例。
我试过在脚本以下/控制台:
>> $CLIENT.respond_to?(:my_thoughts)
=> true
>> $CLIENT.send(:my_thoughts, 'bob', 5)
=> #<#<Class:01xe229be>:0x241391>
>> Thought.send(:my_thoughts, 'bob', 5)
ArgumentError: wrong # of arguments(1 for 2)
from [filepath]:50:in `method_missing'
from (irb):4
有什么非常明显,我在这里失踪?我在Rails 2.3.8和jRuby上运行它,如果这有所帮助的话。
编辑:这混淆我更:
>> Thought.send(:my_thoughts, 'bob', 5, 5)
ArgumentError: wrong # of arguments(3 for 2)
from [filepath]:50:in `method_missing'
from (irb):23
比整数似乎工作以外的东西更换第二个参数,但是当然的说法应该是一个整数...我我现在怀疑jRuby或集成到此的Java类中存在问题。
感谢您的编辑 - 我想说,这不是整个故事。我测试了上面给出的代码,并且它工作正常。来自具有method_missing的文件的实际行50将显示很多。 – 2010-11-04 03:35:08