我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
用 Integer 以外的其他参数替换第二个参数似乎可行,但当然该参数应该是一个 Integer ...我现在怀疑 jRuby 或我集成到其中的 Java 类中存在问题。