Issue #15381 has been updated by ted (Ted Johansson).


I came here to file this feature request, only to find this had already been proposed. This would be beautiful, indeed. :-)

----------------------------------------
Feature #15381: Let double splat call `to_hash` implicitly
https://bugs.ruby-lang.org/issues/15381#change-81623

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
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_hash` 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/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>