From: eregontp@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:95024] [Ruby master Feature#15381] Let double splat call `to_hash` implicitly
Date: Sat, 21 Sep 2019 21:05:52 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-81651.20190921210551.727b306e77791105@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15381.20181205082504@ruby-lang.org
Issue #15381 has been updated by Eregon (Benoit Daloze).
`nil` does not respond to `to_hash` though, how do you propose to deal with that?
Should `**` call `to_h` rather than `to_hash`, similar to `*` calling `to_a` and not `to_ary`?
Your comment https://bugs.ruby-lang.org/issues/15381#note-1 shows that, but I read the mailing list and that doesn't include the edit.
----------------------------------------
Feature #15381: Let double splat call `to_hash` implicitly
https://bugs.ruby-lang.org/issues/15381#change-81651
* 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/
prev parent reply other threads:[~2019-09-21 21:06 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <redmine.issue-15381.20181205082504@ruby-lang.org>
2018-12-05 8:25 ` [ruby-core:90304] [Ruby trunk Feature#15381] Let double splat call `to_hash` implicitly sawadatsuyoshi
2018-12-05 8:28 ` [ruby-core:90305] " sawadatsuyoshi
2018-12-05 12:58 ` [ruby-core:90310] " shevegen
2019-09-20 2:10 ` [ruby-core:94995] [Ruby master " drenmi
2019-09-21 21:05 ` eregontp [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-81651.20190921210551.727b306e77791105@ruby-lang.org \
--to=ruby-core@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).