Project

General

Profile

Actions

Bug #12717

closed

Optional argument treated as kwarg

Added by AMHOL (Andy Holland) over 7 years ago. Updated over 4 years ago.

Status:
Closed
Target version:
-
[ruby-core:77121]

Description

When you define a method with an optional argument and keyword arguments (whether explicitly or with options splat) the defaulted argument can not take a hash argument, instead it is interpreted as keyword arguments:

class Foo
  def self.options(value = nil, **options)
    puts value.inspect
    puts options.inspect
  end

  def self.kwarg(value = nil, kw: nil)
    puts value.inspect
    puts kw.inspect
  end

  def self.splat(*args, kw: nil)
    puts args.inspect
    puts kw.inspect
  end
end

Foo.options({})
# nil
# {}
Foo.kwarg({})
# nil
# nil
Foo.splat({})
# []
# nil

Foo.options({ key: :value })
# nil
# {:key=>:value}
Foo.kwarg({ key: :value })
# ArgumentError: unknown keyword: key
Foo.splat({ key: :value })
# ArgumentError: unknown keyword: key

I would expect the output to be:

Foo.options({})
# {}
# {}
Foo.kwarg({})
# {}
# nil
Foo.splat({})
# [{}]
# nil

Foo.options({ key: :value })
# {:key=>:value}
# {}
Foo.kwarg({ key: :value })
# {:key=>:value}
# nil
Foo.splat({ key: :value })
# [{:key=>:value}]
# nil

Related issues 2 (0 open2 closed)

Related to Ruby master - Feature #14183: "Real" keyword argumentClosedActions
Has duplicate Ruby master - Bug #13336: Default Parameters don't workClosedActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0