Ruby 扩展对象类的send方法
我想更改Ruby中的send方法。我的代码如下Ruby 扩展对象类的send方法,ruby,Ruby,我想更改Ruby中的send方法。我的代码如下 class A def send(symbol, *args) #customize code here #finally call the orinial __send__ function __send__(symbol, args) end end 但是,当我调用诸如obj.send('a_var=',10)之类的send函数时,我得到了以下错误: ArgumentError: wrong number
class A
def send(symbol, *args)
#customize code here
#finally call the orinial __send__ function
__send__(symbol, args)
end
end
但是,当我调用诸如obj.send('a_var=',10)之类的send函数时,我得到了以下错误:
ArgumentError: wrong number of arguments (1 for 0)
错误出现在line call _u_; send _;函数中。
因此,如何修复此错误。如果要将
*args
作为单个参数而不是数组传递到\uuuuuuuuuuuuuuuuuu
调用,您还需要在那里解构它:
__send__(symbol, *args)
对我来说,您的代码是可以的:
class A
def send(symbol, *args)
#customize code here
#finally call the orinial __send__ function
p 'this method has been called'
__send__(symbol, args)
end
def show=(m)
p m
end
end
A.new.send('show=',1,3,4)
A.new.send('show=',1)
A.new.send(:show=,1)
输出:
向我们展示函数体
a_var=
和完整的错误堆栈。什么类的对象是ob
?谢谢你,我已经按照你的建议进行了更改,终于成功了。
"this method has been called"
[1, 3, 4]
"this method has been called"
[1]
"this method has been called"
[1]