ruby 双 * (splat) 运算符有什么作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18289152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:07:32  来源:igfitidea点击:

What does a double * (splat) operator do

rubysplat

提问by Roy Lee

Have you seen a function declared like this?

你见过这样声明的函数吗?

def foo a, **b
  ...
end

I understand that a single *is the splat operator. What does **mean?

我知道单*是 splat 操作员。什么**意思?

回答by Dogbert

Ruby 2.0 introduced keyword arguments, and **acts like *, but for keyword arguments. It returns a Hash with key / value pairs.

Ruby 2.0 引入了关键字参数,其**作用类似于*, 但用于关键字参数。它返回一个带有键/值对的哈希。

For this code:

对于此代码:

def foo(a, *b, **c)
  [a, b, c]
end

Here's a demo:

这是一个演示:

> foo 10
=> [10, [], {}]
> foo 10, 20, 30
=> [10, [20, 30], {}]
> foo 10, 20, 30, d: 40, e: 50
=> [10, [20, 30], {:d=>40, :e=>50}]
> foo 10, d: 40, e: 50
=> [10, [], {:d=>40, :e=>50}]

回答by Daniel Rikowski

That is the double splatoperator which is available since Ruby 2.0.

这是自 Ruby 2.0 以来可用的双 splat运算符。

It captures all keyword arguments(which can also be a simple hash, which was the idiomatic way to emulate keyword arguments before they became part of the Ruby language)

它捕获所有关键字参数(也可以是简单的散列,这是在关键字参数成为 Ruby 语言的一部分之前模拟关键字参数的惯用方式)

def my_method(**options)
  puts options.inspect
end

my_method(key: "value")

The above code prints {key:value}to the console.

上面的代码打印{key:value}到控制台。

Just like the single splat operator captures all regular arguments, but instead of an arrayyou get a hash.

就像单个 splat 运算符捕获所有常规参数一样,但是您得到的是hash而不是数组

Real-life example:

现实生活中的例子:

For example in Rails the cyclemethod looks like this:

例如在 Rails 中,该cycle方法如下所示:

def cycle(first_value, *values)
  options = values.extract_options!
  # ...
end

This method can be called like this: cycle("red", "green", "blue", name: "colors").

这种方法可以这样调用:cycle("red", "green", "blue", name: "colors")

This is quite a common pattern: You accept a list of arguments and the last one is an options hash, which can be extract - for example - using ActiveSupport's extract_options!.

这是一个很常见的模式:你接受一个参数列表,最后一个是一个选项哈希,它可以被提取 - 例如 - 使用 ActiveSupport 的extract_options!.

In Ruby 2.0 you can simplify these methods:

在 Ruby 2.0 中,您可以简化这些方法:

def cycle(first_value, *values, **options)
  # Same code as above without further changes!
end

Admittedly it's only a minor improvement if you are already using ActiveSupport but for plain Ruby the code gains quite a lot of conciseness.

诚然,如果您已经在使用 ActiveSupport,这只是一个很小的改进,但是对于普通的 Ruby,代码获得了相当多的简洁性。

回答by kuboon

In addition, you can use it in caller side like this:

此外,您可以像这样在调用方使用它:

def foo(opts); p opts end
bar = {a:1, b:2}

foo(bar, c: 3)
=> ArgumentError: wrong number of arguments (given 2, expected 1)

foo(**bar, c: 3)
=> {:a=>1, :b=>2, :c=>3}