From: joshua.goodall@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:94920] [Ruby master Feature#16102] `Symbol#call`
Date: Thu, 12 Sep 2019 09:25:43 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-81534.20190912092543.260ca2ea487a90e7@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16102.20190814065946@ruby-lang.org
Issue #16102 has been updated by inopinatus (Joshua GOODALL).
I propose this general solution.
diff --git a/array.c b/array.c
index 3717c3ff34..3809af01cf 100644
--- a/array.c
+++ b/array.c
@@ -6988,6 +6988,7 @@ Init_Array(void)
rb_define_method(rb_cArray, "dig", rb_ary_dig, -1);
rb_define_method(rb_cArray, "sum", rb_ary_sum, -1);
+ rb_define_method(rb_cArray, "splat", rb_yield_splat, 0);
rb_define_method(rb_cArray, "deconstruct", rb_ary_deconstruct, 0);
id_random = rb_intern("random");
then:
arrays = [["a", "b"], ["c"], ["d", "e"]]
arrays.splat(&:product)
#=> [["a", "c", "d"], ["a", "c", "e"], ["b", "c", "d"], ["b", "c", "e"]]
----------------------------------------
Feature #16102: `Symbol#call`
https://bugs.ruby-lang.org/issues/16102#change-81534
* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
Since symbols have a `to_proc` method, it is natural to expect that they would appear in a method chain like:
```ruby
:some_symbol.to_proc.call(...)
```
In fact, I have use cases like this:
```ruby
arrays = [["a", "b"], ["c"], ["d", "e"]]
hashes = [{"a" => 1}, {"b" => 2, "c" => 3}, {"d" => 4, "e" => 5}]
:product.to_proc.(*arrays) # => [["a", "c", "d"], ["a", "c", "e"], ["b", "c", "d"], ["b", "c", "e"]]
:zip.to_proc.(*arrays) # => [["a", "c", "d"], ["b", nil, "e"]]
:union.to_proc.(*arrays) # => ["a", "b", "c", "d", "e"]
:merge.to_proc.(*hashes) # => {"a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5}
```
I request `Symbol#call` to be defined, which would implicitly call `to_proc` on the receiver and then the conventional `Proc#call` on the result. Then, I can do:
```ruby
:product.(*arrays) # => [["a", "c", "d"], ["a", "c", "e"], ["b", "c", "d"], ["b", "c", "e"]]
:zip.(*arrays) # => [["a", "c", "d"], ["b", nil, "e"]]
:union.(*arrays) # => ["a", "b", "c", "d", "e"]
:merge.(*hashes) # => {"a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5}
```
This would solve what proposals #6499, #6727, #7444, #8970, #11262 aim to do.
Notice that proposals #12115 and #15301 ask for `Symbol#call`, but they ask for different things (a method that returns a proc), and are irrelevant to the current proposal.
--
https://bugs.ruby-lang.org/
prev parent reply other threads:[~2019-09-12 9:25 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <redmine.issue-16102.20190814065946@ruby-lang.org>
2019-08-14 6:59 ` [ruby-core:94342] [Ruby master Feature#16102] `Symbol#call` sawadatsuyoshi
2019-08-14 7:00 ` [ruby-core:94343] " sawadatsuyoshi
2019-08-14 8:37 ` [ruby-core:94345] " shevegen
2019-08-14 9:00 ` [ruby-core:94347] " mame
2019-09-12 9:25 ` joshua.goodall [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-81534.20190912092543.260ca2ea487a90e7@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).