summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog7
-rw-r--r--NEWS4
-rw-r--r--eval.c1
-rw-r--r--object.c30
-rw-r--r--test/ruby/test_symbol.rb7
5 files changed, 49 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 5119ab0b01..38458917c4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+Tue Apr 22 21:24:32 2008 Akinori MUSHA <knu@iDaemons.org>
+
+ * eval.c (rb_proc_new): Turn the BLOCK_LAMBDA flag on.
+
+ * object.c (sym_to_proc), test/ruby/test_symbol.rb: Add back
+ Symbol#to_proc, now that it passes the tests.
+
Tue Apr 22 19:35:03 2008 Akinori MUSHA <knu@iDaemons.org>
* enumerator.c (enumerator_initialize): Remove an undocumented
diff --git a/NEWS b/NEWS
index 0a8fe6836b..44ddaf05d6 100644
--- a/NEWS
+++ b/NEWS
@@ -232,6 +232,10 @@ with all sufficient information, see the ChangeLog file.
Return an enumerator if no block is given.
+ * Symbol#to_proc
+
+ New method.
+
* __method__
New global function that returns the name of the current method as
diff --git a/eval.c b/eval.c
index e439768090..a9eb3d38c1 100644
--- a/eval.c
+++ b/eval.c
@@ -9636,6 +9636,7 @@ rb_proc_new(func, val)
Data_Get_Struct(proc, struct BLOCK, data);
data->body->nd_state = YIELD_FUNC_AVALUE;
+ data->flags |= BLOCK_LAMBDA;
return proc;
}
diff --git a/object.c b/object.c
index 28b7e4c84e..1d38798005 100644
--- a/object.c
+++ b/object.c
@@ -1212,6 +1212,35 @@ sym_to_sym(sym)
}
+static VALUE
+sym_call(args, mid)
+ VALUE args, mid;
+{
+ VALUE obj;
+
+ if (RARRAY(args)->len < 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));
+}
+
+
/***********************************************************************
*
* Document-class: Module
@@ -2755,6 +2784,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..ac73dc9137 100644
--- a/test/ruby/test_symbol.rb
+++ b/test/ruby/test_symbol.rb
@@ -74,4 +74,11 @@ 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)
+ assert_nothing_raised(ArgumentError) { :object_id.to_proc.call([]) }
+ assert_nothing_raised(ArgumentError) { :object_id.to_proc.call([1]) }
+ assert_nothing_raised(ArgumentError) { :object_id.to_proc.call([1,2]) }
+ end
end