0
我想的方法添加到C类红宝石传递参数的方法
class C
end
我定义一个PROC:
impl = proc{|x,y| puts "x=#{x} - y=#{y}"}
我添加PROC到类作为方法foo
:
C.send(:define_method, :foo, lambda do |args = impl.parameters.map { |arg| arg[1] }|
puts "foo is called"
impl.call(args)
end
)
当我打电话foo
为C.new.foo(1,2)
,我得到一个错误:
ArgumentError: wrong number of arguments (2 for 0..1)
为了避免这种情况,我需要调用foo
像C.new.foo([1,2])
。有人可以告诉我如何避免这个问题?
咦?你能否定义一个“正常”的方法? –
您是否尝试过C.new.foo(1,2)? – Kagelmacher
啊,当然。我需要更多的咖啡:)不过,我的问题是,这种复杂性是否值得?用常规方法你不会有任何问题,只是说。 –