Alexander Popov
Alexander Popov

Reputation: 24965

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:

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?

Upvotes: 4

Views: 365

Answers (1)

user513951
user513951

Reputation: 13690

I cded into my Ruby install directory and ran grep -r ', \*\*' . and discovered that yes, there are methods in the stdlib that use **kwargs, but only in the open3.rb library.

./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; turns out there are at least a few methods in the C core library that use keyword 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

Upvotes: 3

Related Questions