我想覆盖发送Object,就像这样
class Object
@@object_send = self.instance_method( :send )
def send *args
@@object_send.bind( self ).call *args
end
end
或
class Object
def send *args
__send__ *args
end
end
或
class Object
alias_method :old_send, :send
def send *args
old_send *args
end
end
但所有这些选项都会导致出现此错误
/opt/local/lib/ruby1.9/gems/1.9.1/gems/minitest-2.8.1/lib/minitest/unit.rb:871:in `block in process_args': unsupported argument type: Integer (ArgumentError)
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/minitest-2.8.1/lib/minitest/unit.rb:862:in `new'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/minitest-2.8.1/lib/minitest/unit.rb:862:in `process_args'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/minitest-2.8.1/lib/minitest/unit.rb:912:in `_run'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/minitest-2.8.1/lib/minitest/unit.rb:905:in `run'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/minitest-2.8.1/lib/minitest/unit.rb:685:in `block in autorun'
我能做些什么吗?
更新:尝试更新到2.9.1,但尚未解决问题
答案 0 :(得分:1)
如果没有MWE,我很难分析您的问题。也许我找到了你的问题的第一个提示。
我尝试重建错误,但没有成功:
class Object
alias_method :old_send, :send
def send *args
old_send *args
end
end
n = 5
puts n.send(:*, 2)
我得到10
。
但是我遇到了问题:
puts n.send(:times){ |i| p i } #-> #<Enumerator:0xb778a8>
稍加修改即可看出会发生什么:
class Object
alias_method :old_send, :send
def send *args
puts "send called with #{args}" #--> [:times]
old_send *args
end
end
n = 5
n.send(:times){ |i| p i }
你得到了
send called with [:times]
缺少该块。您必须将proc参数添加到重新定义:
class Object
alias_method :old_send, :send
def send *args, &proc
old_send *args, &proc
end
end
n = 5
n.send(:times){ |i| p i } #-> 1 2 3 4 5