2

我知道这**args被解释为包含传递给函数的所有键值对的哈希,但我不明白为什么它比典型参数更受欢迎。例如,我有以下两个功能。

def test(some_string, hash)
    puts hash
    puts hash.class # => Hash
end


def test_two(some_string, **hash)
    puts hash
    puts hash.class # => Hash
end 

调用test("test string", a: 1, b: 2)test_two("test string", a: 1, b: 2)产生完全相同的结果。**用作参数值有什么好处?

4

2 回答 2

3

Ruby 2.7 开始更清楚地区分关键字参数和常规哈希。**args用于关键字参数。一些影响:

def test3(some_string, foo:, **args)
  puts args
end

test3('a', foo: 'b', bar: 'c') # => {:bar=>"c"}

但是按预期工作

def test3(some_string, foo:, hash)
  puts args
end # => syntax error

def test3(some_string, hash, foo:)
  puts args
end # works so far

test3('a', foo: 'b', bar: 'c')
# warning: Passing the keyword argument as the last hash parameter is deprecated
# ArgumentError (missing keyword: :foo)

一旦升级到 ruby​​ 3,警告就会变成错误。

于 2021-01-22T21:31:40.863 回答
1

使用双 splat 运算符**作为参数的好处主要是您可以完全避免传递任何参数。与使用单个 splat 运算符时发生的情况非常相似*

使用您的示例并调用:

test "s"
# raises ArgumentError (wrong number of arguments (given 1, expected 2))

test_two "s"
# works, prints `{} Hash`

这在您希望有一个“主要”参数的方法中很有用,通常是一些选项,而不会使参数空间混乱。例如,想象一个 CSV 行解析器:

def parse_row_1(row, **options)
  separator = options.fetch :separator, ","
  quote_char = options.fetch :quote_char, null
  # ...
end

def parse_row_2(row, separator = ",", quote_char = null)
  # ...
end

# To parse a string like this:
s = "'ABC','123','DEF'"

# With ** method you can do just this:
parse_row_1 s, quote_char: "'"

# Without ** you must specify every time the arguments, because they are positional:
parse_row_2 s, ",", "'"
于 2021-01-23T04:16:31.380 回答