ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:77121] [Ruby trunk Bug#12717] Optional argument treated as kwarg
       [not found] <redmine.issue-12717.20160901143642@ruby-lang.org>
@ 2016-09-01 14:36 ` andyholland1991
  2018-09-05 15:22 ` [ruby-core:88862] " ruby-core
  2019-08-30 21:46 ` [ruby-core:94693] [Ruby master " merch-redmine
  2 siblings, 0 replies; 3+ messages in thread
From: andyholland1991 @ 2016-09-01 14:36 UTC (permalink / raw
  To: ruby-core

Issue #12717 has been reported by Andy Holland.

----------------------------------------
Bug #12717: Optional argument treated as kwarg
https://bugs.ruby-lang.org/issues/12717

* Author: Andy Holland
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
* ruby -v: 2.3.1
* Backport: 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN
----------------------------------------
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:

~~~ ruby
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:

~~~ ruby
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
~~~




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [ruby-core:88862] [Ruby trunk Bug#12717] Optional argument treated as kwarg
       [not found] <redmine.issue-12717.20160901143642@ruby-lang.org>
  2016-09-01 14:36 ` [ruby-core:77121] [Ruby trunk Bug#12717] Optional argument treated as kwarg andyholland1991
@ 2018-09-05 15:22 ` ruby-core
  2019-08-30 21:46 ` [ruby-core:94693] [Ruby master " merch-redmine
  2 siblings, 0 replies; 3+ messages in thread
From: ruby-core @ 2018-09-05 15:22 UTC (permalink / raw
  To: ruby-core

Issue #12717 has been updated by marcandre (Marc-Andre Lafortune).


I believe this is as designed.

As I stated previously (https://bugs.ruby-lang.org/issues/11967#note-3), my understanding is that:

* after all mandatory unnamed arguments are filled
* if the last remaining argument is hash-like
* and all its keys are symbols
* and the method called uses keyword arguments => then that parameter is used for keyword arguments.

I believe we should close this issue.

----------------------------------------
Bug #12717: Optional argument treated as kwarg
https://bugs.ruby-lang.org/issues/12717#change-73900

* Author: AMHOL (Andy Holland)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
* ruby -v: 2.3.1
* Backport: 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN
----------------------------------------
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:

~~~ ruby
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:

~~~ ruby
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
~~~




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [ruby-core:94693] [Ruby master Bug#12717] Optional argument treated as kwarg
       [not found] <redmine.issue-12717.20160901143642@ruby-lang.org>
  2016-09-01 14:36 ` [ruby-core:77121] [Ruby trunk Bug#12717] Optional argument treated as kwarg andyholland1991
  2018-09-05 15:22 ` [ruby-core:88862] " ruby-core
@ 2019-08-30 21:46 ` merch-redmine
  2 siblings, 0 replies; 3+ messages in thread
From: merch-redmine @ 2019-08-30 21:46 UTC (permalink / raw
  To: ruby-core

Issue #12717 has been updated by jeremyevans0 (Jeremy Evans).

Status changed from Open to Closed

With the changes in #14183:

```ruby
Foo.options({})
# (irb):21: warning: The last argument for `options' (defined at (irb):6) is used as the keyword parameter
# nil
# {}

Foo.kwarg({})
# (irb):22: warning: The last argument for `kwarg' (defined at (irb):11) is used as the keyword parameter
# nil
# nil

Foo.splat({})
# (irb):23: warning: The last argument for `splat' (defined at (irb):16) is used as the keyword parameter
# []
# nil

Foo.options({ key: :value })
# (irb):24: warning: The last argument for `options' (defined at (irb):6) is used as the keyword parameter
# nil
# {:key=>:value}

Foo.kwarg({ key: :value })
# (irb):25: warning: The last argument for `kwarg' (defined at (irb):11) is used as the keyword parameter
# ArgumentError (unknown keyword: :key)

Foo.splat({ key: :value })
# (irb):26: warning: The last argument for `splat' (defined at (irb):16) is used as the keyword parameter
# ArgumentError (unknown keyword: :key)
```

In Ruby 3, the behavior will be:

```ruby
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
```

I'm going to close this now as the deprecation warnings for the cases where behavior will change in Ruby 3 have been added.

----------------------------------------
Bug #12717: Optional argument treated as kwarg
https://bugs.ruby-lang.org/issues/12717#change-81299

* Author: AMHOL (Andy Holland)
* Status: Closed
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
* ruby -v: 2.3.1
* Backport: 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN
----------------------------------------
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:

~~~ ruby
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:

~~~ ruby
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
~~~




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-08-30 21:46 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <redmine.issue-12717.20160901143642@ruby-lang.org>
2016-09-01 14:36 ` [ruby-core:77121] [Ruby trunk Bug#12717] Optional argument treated as kwarg andyholland1991
2018-09-05 15:22 ` [ruby-core:88862] " ruby-core
2019-08-30 21:46 ` [ruby-core:94693] [Ruby master " merch-redmine

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).