From: shevegen@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:90310] [Ruby trunk Feature#15381] Let double splat call `to_hash` implicitly
Date: Wed, 05 Dec 2018 12:58:45 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-75414.20181205125843.c09a2a7bd5bb3f12@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15381.20181205082504@ruby-lang.org
Issue #15381 has been updated by shevegen (Robert A. Heiler).
I myself have used *foobar quite a lot in ruby code, such as in:
def foobar(*args)
args.each # and do something then
end
and I have also used &: considerably often too. The most frequent
use case for me personally is to use &: together with .map(). This
is an area where I actually prefer e. g. .map(&:strip) as opposed
to something like .map {|line| line.strip } or something like
that. While I consider the second variant more readable to me,
the &: variant is significantly shorter. (& is not very pretty
though so I try to not use it too often).
I have not yet used **, I think (strangely enough; perhaps I have not
needed it so far). So I can not say much about the proposal itself
either way. I am both clueless and neither pro nor con. :)
I agree with the above reasoning of nil.to_h which makes sense (if
the functionality in itself is approved and I guess we have to ask
matz about this).
I think this is where matz has to decide whether ** should behave as
described, stay as it is (status quo), or have some other (implicit?)
meaning that was not yet mentioned. I really can not say either way,
but I think what is also said in the issue here is that * has a better
defined meaning right now than does **. So this is where I think matz
has to decide either way.
I would recommend adding this suggestion to an upcoming developer meeting,
but perhaps not for 2018 but 2019 instead - last dev meeting this year
should ideally be for the ruby x-mas release. :D
On a side note, does anyone have one or more good or simple use cases
for **? I am trying to find an example for where it may be used but
I do not have any local example.
Last but not least, although I understand that the example given was
mostly to illustrate a point, so that's fine; the * and ** variants
with () and conditionals, are a bit ugly. :P
I understand it is the illustration of an example but I really hope
people don't write code such as "**({a: 1} if some_condition)"; it
takes my brain quite some time to process.
----------------------------------------
Feature #15381: Let double splat call `to_hash` implicitly
https://bugs.ruby-lang.org/issues/15381#change-75414
* 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/
next prev parent reply other threads:[~2018-12-05 12:58 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 ` shevegen [this message]
2019-09-20 2:10 ` [ruby-core:94995] [Ruby master " drenmi
2019-09-21 21:05 ` [ruby-core:95024] " eregontp
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-75414.20181205125843.c09a2a7bd5bb3f12@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).