【发布时间】:2010-11-04 02:28:51
【问题描述】:
我在模型中实现了以下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 行会显示很多内容。
标签: ruby-on-rails jruby method-missing