From: "Eregon (Benoit Daloze) via ruby-core" Date: 2024-10-24T11:33:15+00:00 Subject: [ruby-core:119605] [Ruby master Feature#15381] Let double splat call `to_h` implicitly Issue #15381 has been updated by Eregon (Benoit Daloze). Regarding `**@`, I'm not a big fan because `**` on the caller side is very much syntax a bit like a keyword, and it has the meaning the pass the Hash/expression as keyword arguments and not as a position argument. Of course you could have `**` the syntax and `**@` the unary operator called by it, but effectively it's not really an operator in the sense of unary +/- or binary operators, e.g. it can only be used in calls and in Hash literals. Regarding trying `to_hash` then `to_h` or vice-versa it will have some overhead for the second method being tried for non-Hash. `foo(**obj.to_h)` doesn't sound too bad to me as I would expect it's needed in only a few places (compared to all `**hash` usages) and it's probably clearer what is happening that way if `obj` is not a Hash/to_hash. ---------------------------------------- Feature #15381: Let double splat call `to_h` implicitly https://bugs.ruby-lang.org/issues/15381#change-110227 * Author: sawa (Tsuyoshi Sawada) * Status: Open ---------------------------------------- The single splat calls `to_a` implicitly on the object (if it is not an array already) so that, for example, we have the convenience of writing conditions in an array literal: ```ruby a = [ *(:foo if some_condition), *(:bar if another_condition), ] ``` And the ampersand implicitly calls `to_proc` on the object (if it is not a proc already) so that we can substitute a block with an ampersand followed by a symbol: ```ruby some_method(&:some_method_name) ``` Unlike the single splat and ampersand, the double splat does not seem to implicitly call a corresponding method. I propose that the double splat should call `to_h` implicitly on the object if it not already a Hash so that we can, for example, write a condition in a hash literal as follows: ```ruby h = { **({a: 1} if some_condition), **({b: 2) if another_condition), } ``` There may be some other benefits of this feature that I have not noticed yet. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/