【发布时间】:2021-03-11 19:15:39
【问题描述】:
我有几个方法接受许多(关键字)参数,最终将同一组参数传递给另一个方法。
以下是正常的。
def foo(a:, b:, c:, d:, e:)
bar('var', a:a, b:b, c:c, d:d, e:e)
end
# takes the same arguments as #foo + one more
def bar(var, a:, b:, c:, d:, e:)
...
end
这有点乏味和烦人。我想知道 Ruby 核心中是否有任何东西可以轻松执行以下操作...
def foo(a:, b:, c:, d:, e:)
bar('var', <something that automagically collects all of the keyword args>)
end
我知道您可以解析 method(__method__).parameters,做一些体操,然后将所有内容打包成一个哈希,然后可以双拼并传递给 bar。我只是想知道核心中是否已经有一些东西可以以一种很好、简洁的方式做到这一点?
如果有一些东西以更一般的方式应用,即不仅适用于关键字 args,那么这当然也很有趣。
【问题讨论】:
标签: ruby keyword-argument