Skip to content

Instantly share code, notes, and snippets.

@wanabe
Created March 3, 2012 17:30
Show Gist options
  • Save wanabe/1967077 to your computer and use it in GitHub Desktop.
Save wanabe/1967077 to your computer and use it in GitHub Desktop.
[1,2,10].map(&:to_s(16)) # => ["1", "2", "a"]
Index: parse.y
===================================================================
--- parse.y (revision 34892)
+++ parse.y (working copy)
@@ -2543,6 +2543,10 @@ block_arg : tAMPER arg_value
$$ = $2;
%*/
}
+ | tAMPER literal paren_args
+ {
+ $$ = NEW_BLOCK_PASS(NEW_CALL($2, rb_intern("to_proc"), $3));
+ }
;
opt_block_arg : ',' block_arg
Index: string.c
===================================================================
--- string.c (revision 34892)
+++ string.c (working copy)
@@ -7515,6 +7515,27 @@ sym_call(VALUE args, VALUE sym, int argc, VALUE *argv)
return rb_funcall_passing_block(obj, (ID)sym, argc - 1, argv + 1);
}
+static VALUE
+sym_call2(VALUE arg, VALUE args, int argc, VALUE *argv)
+{
+ VALUE obj;
+ VALUE method;
+ int i;
+
+ if (argc < 1) {
+ rb_raise(rb_eArgError, "no receiver given");
+ }
+ obj = argv[0];
+
+ args = rb_ary_dup(args);
+ method = rb_ary_shift(args);
+ for (i = 1; i < argc; i++) {
+ rb_ary_push(args, argv[i]);
+ }
+
+ return rb_funcall_passing_block(obj, SYM2ID(method), RARRAY_LEN(args), RARRAY_PTR(args));
+}
+
/*
* call-seq:
* sym.to_proc
@@ -7525,13 +7546,23 @@ sym_call(VALUE args, VALUE sym, int argc, VALUE *argv)
*/
static VALUE
-sym_to_proc(VALUE sym)
+sym_to_proc(int argc, VALUE *argv, VALUE sym)
{
static VALUE sym_proc_cache = Qfalse;
enum {SYM_PROC_CACHE_SIZE = 67};
VALUE proc;
long id, index;
VALUE *aryp;
+ VALUE args;
+
+ if (argc > 0) {
+ args = rb_ary_new2(argc + 1);
+ rb_ary_push(args, sym);
+ for (index = 0; index < argc; index++) {
+ rb_ary_push(args, argv[index]);
+ }
+ return rb_proc_new(sym_call2, args);
+ }
if (!sym_proc_cache) {
sym_proc_cache = rb_ary_tmp_new(SYM_PROC_CACHE_SIZE * 2);
@@ -7909,7 +7940,7 @@ Init_String(void)
rb_define_method(rb_cSymbol, "id2name", rb_sym_to_s, 0);
rb_define_method(rb_cSymbol, "intern", sym_to_sym, 0);
rb_define_method(rb_cSymbol, "to_sym", sym_to_sym, 0);
- rb_define_method(rb_cSymbol, "to_proc", sym_to_proc, 0);
+ rb_define_method(rb_cSymbol, "to_proc", sym_to_proc, -1);
rb_define_method(rb_cSymbol, "succ", sym_succ, 0);
rb_define_method(rb_cSymbol, "next", sym_succ, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment