Ruby告诉我传递了一个参数(应该是0)。我希望**{}是等同的。我的想法中的错误在哪里?
class Test
def takenothing() end
end
def wrapper( *args, **keys, &block )
keys.delete :key
Test.new.send :takenothing, *args, **keys, &block
end
wrapper key: 'nyeh'发布于 2016-09-02 03:29:46
好了,让我们来看看:
def wrapper(*args, **keys, &block)
keys.delete :key
p **keys好的:
$ ruby test.rb
{}所以,不,当调用一个方法时,**{}不会将它从参数列表中删除,它只是计算为一个空的散列。
看看StackOverflow上的另一个question,以及相应的Ruby bug report。
发布于 2016-09-02 03:14:03
出现错误的原因是因为send方法使用参数调用takenothing方法,而takenothing方法不接受任何参数。
Test.new.send :takenothing, *args, **keys, &block
Test.new.send symbol [, args...])和你的方法
def takenothing() end # this method takes no argumenthttps://stackoverflow.com/questions/39278662
复制相似问题