summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--NEWS4
-rw-r--r--object.c29
-rw-r--r--test/ruby/test_symbol.rb4
4 files changed, 42 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index b616d7dc7c..dec1703464 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Sun Apr 13 15:55:52 2008 Kazuhiro NISHIYAMA <zn@mbf.nifty.com>
+
+ * object.c (sym_to_proc): new method Symbol#to_proc; backported
+ from 1.9. bug#19012
+
Fri Apr 11 19:14:30 2008 Kazuhiro NISHIYAMA <zn@mbf.nifty.com>
* object.c (rb_obj_tap): new method Object#tap; backported from
diff --git a/NEWS b/NEWS
index 7f70f105aa..ab27f9fb80 100644
--- a/NEWS
+++ b/NEWS
@@ -71,6 +71,10 @@ with all sufficient information, see the ChangeLog file.
New exception class that causes Kernel#loop to stop iteration when
raised.
+ * Symbol#to_proc
+
+ New method.
+
* enumerator
* Enumerator is now a built-in module. The #next and #rewind
diff --git a/object.c b/object.c
index cd21f4efe6..c67381d5ec 100644
--- a/object.c
+++ b/object.c
@@ -1205,6 +1205,34 @@ sym_to_sym(sym)
return sym;
}
+static VALUE
+sym_call(args, mid)
+ VALUE args, mid;
+{
+ VALUE obj;
+
+ if (RARRAY_LEN(args) < 1) {
+ rb_raise(rb_eArgError, "no receiver given");
+ }
+ obj = rb_ary_shift(args);
+ return rb_apply(obj, (ID)mid, args);
+}
+
+/*
+ * call-seq:
+ * sym.to_proc
+ *
+ * Returns a _Proc_ object which respond to the given method by _sym_.
+ *
+ * (1..3).collect(&:to_s) #=> ["1", "2", "3"]
+ */
+
+static VALUE
+sym_to_proc(VALUE sym)
+{
+ return rb_proc_new(sym_call, (VALUE)SYM2ID(sym));
+}
+
/***********************************************************************
*
@@ -2749,6 +2777,7 @@ Init_Object()
rb_define_method(rb_cSymbol, "to_s", sym_to_s, 0);
rb_define_method(rb_cSymbol, "id2name", sym_to_s, 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, "===", rb_obj_equal, 1);
rb_define_method(rb_cModule, "freeze", rb_mod_freeze, 0);
diff --git a/test/ruby/test_symbol.rb b/test/ruby/test_symbol.rb
index 2ccfe64c92..f53e615766 100644
--- a/test/ruby/test_symbol.rb
+++ b/test/ruby/test_symbol.rb
@@ -74,4 +74,8 @@ class TestSymbol < Test::Unit::TestCase
assert_inspect_evaled(':$0')
assert_inspect_evaled(':$1')
end
+
+ def test_to_proc
+ assert_equal %w(1 2 3), (1..3).map(&:to_s)
+ end
end