gpt4 book ai didi

ruby - 在 Ruby 中跳过额外的关键字参数

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

我定义了一个方法:

def method(one: 1, two: 2)
[one, two]
end

当我这样调用它时:

method one: 'one', three: 'three'

我得到:

ArgumentError: unknown keyword: three

我不想从散列中一个一个地提取所需的键或排除额外的键。除了像这样定义方法之外,有没有办法规避这种行为:

def method(one: 1, two: 2, **other)
[one, two, other]
end

最佳答案

如果不想写**other中的other,可以省略。

def method(one: 1, two: 2, **)
[one, two]
end

关于ruby - 在 Ruby 中跳过额外的关键字参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30942133/

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