0

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)

エラーは行呼び出し __ send__ 関数にあります。では、どうすればこのエラーを修正できますか。

4

2 に答える 2

1

私にとってあなたのコードは大丈夫です:

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)

出力:

"this method has been called"
[1, 3, 4]
"this method has been called"
[1]
"this method has been called"
[1]
于 2013-04-05T06:45:51.710 に答える