[ruby-core:94719] [Ruby master Bug#14909] Method call with object that has to_hash method crashes (method with splat and keyword arguments)

From: merch-redmine@...
Date: 2019-09-02 04:48:03 UTC
List: ruby-core #94719
Issue #14909 has been updated by jeremyevans0 (Jeremy Evans).

Status changed from Feedback to Closed

The changes in #14183 solve this issue.  You will now get warnings:

```ruby
my_func(Test.new)
# (irb):101: warning: The last argument is used as the keyword parameter
# (irb):92: warning: for `my_func' defined here
# ArgumentError (unknown keyword: :to_hash_key)
```

In Ruby 3, this will be passed as a positional argument.  To get the Ruby 3 behavior with the master branch:


```ruby
my_func(Test.new, **(;{}))
```

----------------------------------------
Bug #14909: Method call with object that has to_hash method crashes (method with splat and keyword arguments)
https://bugs.ruby-lang.org/issues/14909#change-81332

* Author: johannes_luedke (Johannes Lke)
* Status: Closed
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin17]
* Backport: 2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN
----------------------------------------
In a method with a splat method argument followed by a keyword argument, it leads to an `ArgumentError` when calling the method with an object that reacts to `to_hash`


~~~ruby
def my_func(*objects, error_code: 400)
  objects.inspect
end

class Test
  def to_hash
    # an example hash
    { to_hash_key: "to_hash" }
  end
end

my_func(Test.new)
~~~

Observed result: an exception is raised: `in my_func: unknown keyword: to_hash_key (ArgumentError)` 
Expected result: `[#<Test:0x007fc8c9825318>]` is returned by the `my_func` call

It should behave the same when calling with objects that have a `to_hash` method and objects that don't, shouldn't it?



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

In This Thread

Prev Next