gpt4 book ai didi

Ruby - splat 和 send() 混淆

转载 作者:数据小太阳 更新时间:2023-10-29 08:30:30 24 4
gpt4 key购买 nike

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'

最佳答案

好吧,让我们来看看:

def wrapper(*args, **keys, &block)
keys.delete :key
p **keys

确定:

$ ruby test.rb 
{}

所以,不,**{} 在调用方法时不会将其从参数列表中删除,它只是计算为一个空散列。

看看这个其他的question在 StackOverflow 上,以及相应的 Ruby bug report .

关于Ruby - splat 和 send() 混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39278662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com