Project

General

Profile

Actions

Feature #15381

open

Let double splat call `to_h` implicitly

Added by sawa (Tsuyoshi Sawada) over 5 years ago. Updated about 1 year ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:90304]

Description

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:

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:

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:

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.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0