From: Nobuyoshi Nakada <nobu@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:20430] Re: Array#to_proc
Date: Tue, 9 Dec 2008 02:24:15 +0900 [thread overview]
Message-ID: <20081208173046.9579BE09B0@mail.bc9.jp> (raw)
In-Reply-To: <A6715F58-FC1B-4967-A7B7-C0C355D3EFFD@andersground.net>
Hi,
At Mon, 8 Dec 2008 22:21:16 +0900,
Florian Gilcher wrote in [ruby-core:20421]:
> This might make sense for an Array of Symbols. But i've also seen use
> of this
> in other interpretations, like for chaining Arrays of procs. Other
> uses could be
> imagined.
What about Symbol#call()?
(1..3).collect(&:*.(2)) #=> [2, 4, 6]
\f
Index: string.c
===================================================================
--- string.c (revision 20574)
+++ string.c (working copy)
@@ -6944,4 +6944,71 @@ sym_to_proc(VALUE sym)
static VALUE
+yield_call(VALUE dummy, VALUE proc, int argc, VALUE *argv)
+{
+ return rb_proc_call_with_block(proc, argc, argv, Qnil);
+}
+
+static VALUE
+sym_call_with_args(VALUE dummy, VALUE args, int argc, VALUE *argv)
+{
+ VALUE obj, sym, proc, *ptr = RARRAY_PTR(args);
+ long len = RARRAY_LEN(args);
+ volatile VALUE newargs;
+ ID id;
+
+ if (argc < 1) {
+ rb_raise(rb_eArgError, "no receiver given");
+ }
+ proc = ptr[--len];
+ sym = ptr[--len];
+ obj = *argv++;
+ if (!--argc) {
+ argc = len;
+ argv = ptr;
+ }
+ else if (len) {
+ VALUE *p;
+ newargs = rb_ary_tmp_new(argc + len);
+ rb_ary_store(newargs, argc + len - 1, Qnil);
+ p = RARRAY_PTR(newargs);
+ MEMCPY(p, argv, VALUE, argc);
+ MEMCPY(p + argc, ptr, VALUE, len);
+ argv = p;
+ argc += len;
+ }
+ id = SYM2ID(sym);
+ RB_GC_GUARD(args);
+ if (NIL_P(proc)) {
+ return rb_funcall3(obj, id, argc, argv);
+ }
+ else {
+ return rb_block_call(obj, id, argc, argv, yield_call, proc);
+ }
+}
+
+/*
+ * call-seq:
+ * sym.call(*args)
+ * sym.(*args)
+ *
+ * Returns a _Proc_ object which respond to the given method by _sym_ with _args_.
+ *
+ * (1..3).collect(&:*.(2)) #=> [2, 4, 6]
+ */
+
+static VALUE
+sym_to_proc_with_args(int argc, VALUE *argv, VALUE sym)
+{
+ VALUE args, proc = 0;
+ if (rb_block_given_p()) proc = rb_block_proc();
+ if (argc == 0 && !proc) return sym_to_proc(sym);
+ args = rb_ary_tmp_new(argc + 2);
+ rb_ary_store(args, argc + 1, proc);
+ MEMCPY(RARRAY_PTR(args), argv, VALUE, argc);
+ RARRAY_PTR(args)[argc] = sym;
+ return rb_proc_new(sym_call_with_args, args);
+}
+
+static VALUE
sym_succ(VALUE sym)
{
@@ -7214,4 +7281,5 @@ Init_String(void)
rb_define_method(rb_cSymbol, "=~", sym_match, 1);
+ rb_define_method(rb_cSymbol, "call", sym_to_proc_with_args, -1);
rb_define_method(rb_cSymbol, "[]", sym_aref, -1);
rb_define_method(rb_cSymbol, "slice", sym_aref, -1);
\f
--
Nobu Nakada
next prev parent reply other threads:[~2008-12-08 17:34 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-12-08 12:47 [ruby-core:20418] Array#to_proc Eustáquio Rangel
2008-12-08 12:55 ` [ruby-core:20419] Array#to_proc Austin Ziegler
2008-12-08 13:21 ` [ruby-core:20421] " Florian Gilcher
2008-12-08 13:56 ` [ruby-core:20423] " Eustáquio Rangel
2008-12-08 14:08 ` [ruby-core:20424] " James Coglan
2008-12-08 17:24 ` Nobuyoshi Nakada [this message]
2008-12-08 17:51 ` [ruby-core:20433] " Mikael Høilund
2008-12-09 5:58 ` [ruby-core:20439] " Yukihiro Matsumoto
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=20081208173046.9579BE09B0@mail.bc9.jp \
--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).