[ruby-core:76996] [Ruby trunk Bug#10856] Splat with empty keyword args gives unexpected results
From:
eregontp@...
Date:
2016-08-21 15:29:01 UTC
List:
ruby-core #76996
Issue #10856 has been updated by Benoit Daloze.
Guyren Howe wrote:
> I believe this behavior is wrong and should be fixed.
>
> This gets in the way of simple functional programming idioms. eg "Call each of these functions with these args until one doesn't fail"
There is a simple fix for your use-case, if you just want to fowrard arguments, don't use ** at all:
(it's not like in Python, keyword arguments are less separated form normal arguments)
> ~~~ ruby
> class FnSeries
> def initialize(*fns)
> @fns = fns
> end
>
> def call(*args)
> @fns.each do |fn|
> begin
> return fn.call(*args)
> rescue Exception => e
> end
> end
> end
> ~~~
Marc-Andre Lafortune wrote:
> I feel this has to be fixed.
>
> foo(**{}) should === foo(**Hash.new) in all cases, and I feel it should not raise an error.
I agree, it's highly inconsistent that:
~~~ ruby
def foo(*args); args; end
foo(**{}) # => []
h={}
foo(**h) # => [{}]
foo(h) # => [{}]
~~~
----------------------------------------
Bug #10856: Splat with empty keyword args gives unexpected results
https://bugs.ruby-lang.org/issues/10856#change-60220
* Author: Sean Griffin
* Status: Open
* Priority: Normal
* Assignee:
* ruby -v: ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin13]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
When keyword args are passed to a method with splat, and there are no keyword args, an empty hash is sent. I would expect no argument to be given, same as splat with an empty array. For example:
def foo
end
foo(**{})
This causes an argument error, as an empty hash is passed. I would expect the same behavior as
def foo
end
foo(*[])
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>