简体   繁体   English

在Ruby中是否有一个需要关键字参数的内置方法?

[英]In Ruby is there a built-in method that requires keyword arguments?

I know that in Ruby 2.0 and later you can have keyword arguments, so that you can define a method like this: 我知道在Ruby 2.0及更高版本中你可以有关键字参数,这样你就可以定义这样的方法:

def foo(inline_argument, *args, **kwargs, &block)
  puts 'bar'
end

However, I was wondering: is there a built-in method which makes use of keyword arguments? 但是,我想知道:是否有一个使用关键字参数的内置方法?

I cd ed into my Ruby install directory and ran grep -r ', \\*\\*' . cd编入我的Ruby安装目录,然后运行grep -r ', \\*\\*' . and discovered that yes, there are methods in the stdlib that use **kwargs , but only in the open3.rb library. 并且发现是的,stdlib中有一些方法使用**kwargs ,但只在open3.rb库中。

./lib/ruby/2.1.0/open3.rb:  def popen3(*cmd, **opts, &block)
./lib/ruby/2.1.0/open3.rb:  def popen2(*cmd, **opts, &block)
./lib/ruby/2.1.0/open3.rb:  def popen2e(*cmd, **opts, &block)
./lib/ruby/2.1.0/open3.rb:  def capture3(*cmd, stdin_data: '', binmode: false, **opts)
./lib/ruby/2.1.0/open3.rb:  def capture2(*cmd, stdin_data: '', binmode: false, **opts)
./lib/ruby/2.1.0/open3.rb:  def capture2e(*cmd, stdin_data: '', binmode: false, **opts)
./lib/ruby/2.1.0/open3.rb:  def pipeline_rw(*cmds, **opts, &block)
./lib/ruby/2.1.0/open3.rb:  def pipeline_r(*cmds, **opts, &block)
./lib/ruby/2.1.0/open3.rb:  def pipeline_w(*cmds, **opts, &block)
./lib/ruby/2.1.0/open3.rb:  def pipeline_start(*cmds, **opts, &block)
./lib/ruby/2.1.0/open3.rb:  def pipeline(*cmds, **opts)

EDIT : 编辑

At @mdesantis suggestion, I did an MRI identifier search for rb_get_kwargs ; 在@mdesantis建议中,我对rb_get_kwargs进行了MRI标识符搜索 ; turns out there are at least a few methods in the C core library that use keyword args. 事实证明,C核心库中至少有一些使用关键字args的方法。

Dir.new( string, encoding: enc ) -> aDir

Array#sample(n, random: rng) -> new_ary
Array#shuffle!(random: rng) -> ary

GC.start(full_mark: false) -> nil

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM