<-- home

Ruby 参数关键字 **

如果不知道后面可能用到什么参数,最后一个参数可以直接写成 **(Double **Splat),它会接受所有 hash 类型的参数

def test(a, b, **others)
    p a, b, others, others[:test], others[:no_key]
end

test(1, 2, test: 'test string', test_another: 'test another string')

=> 
1
2
{:test=>"test string", :test_another=>"test another string"}
"test string"
nil

参考:

https://flushentitypacket.github.io/ruby/2015/03/31/ruby-keyword-arguments-the-double-splat-and-starsnake.html

https://www.rubyguides.com/2018/06/rubys-method-arguments/

https://dev.firmafon.dk/blog/drat-ruby-has-a-double-splat/