Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 扩展对象类的send方法_Ruby - Fatal编程技术网

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

我想更改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 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]