From: eregontp@... Date: 2018-07-12T12:29:22+00:00 Subject: [ruby-core:87927] [Ruby trunk Bug#14909] Method call with object that has to_hash method crashes (method with splat and keyword arguments) Issue #14909 has been updated by Eregon (Benoit Daloze). At least, it behaves the same if passing a keyword arguments directly: ~~~ruby def my_func(*objects, error_code: 400) objects.inspect end my_func(to_hash_key: "to_hash") # => unknown keyword: to_hash_key (ArgumentError) ~~~ So this has nothing to do with the `to_hash` conversion. One way to workaround this is: ~~~ruby def my_func(*objects, error_code: 400, **kwargs) kwargs end p my_func(to_hash_key: "to_hash") # => {:to_hash_key=>"to_hash"} ~~~ So adding a keyrest argument (**kwargs), because there is already a keyword argument which means keywords have to fit in the declared keyword args, unless there is a keyrest arg. ---------------------------------------- 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-72936 * Author: johannes_luedke (Johannes L��dke) * Status: Feedback * 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: `[#]` 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: