ruby-changes:4656
From: ko1@a...
Date: Tue, 22 Apr 2008 21:43:16 +0900 (JST)
Subject: [ruby-changes:4656] knu - Ruby:r16150 (ruby_1_8): * eval.c (rb_proc_new): Turn the BLOCK_LAMBDA flag on.
knu 2008-04-22 21:42:57 +0900 (Tue, 22 Apr 2008) New Revision: 16150 Modified files: branches/ruby_1_8/ChangeLog branches/ruby_1_8/NEWS branches/ruby_1_8/eval.c branches/ruby_1_8/object.c branches/ruby_1_8/test/ruby/test_symbol.rb Log: * 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. http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ChangeLog?r1=16150&r2=16149&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/NEWS?r1=16150&r2=16149&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/object.c?r1=16150&r2=16149&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/test/ruby/test_symbol.rb?r1=16150&r2=16149&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/eval.c?r1=16150&r2=16149&diff_format=u Index: ruby_1_8/NEWS =================================================================== --- ruby_1_8/NEWS (revision 16149) +++ ruby_1_8/NEWS (revision 16150) @@ -232,6 +232,10 @@ 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 Index: ruby_1_8/ChangeLog =================================================================== --- ruby_1_8/ChangeLog (revision 16149) +++ ruby_1_8/ChangeLog (revision 16150) @@ -1,3 +1,10 @@ +Tue Apr 22 21:24:32 2008 Akinori MUSHA <knu@i...> + + * 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@i...> * enumerator.c (enumerator_initialize): Remove an undocumented Index: ruby_1_8/object.c =================================================================== --- ruby_1_8/object.c (revision 16149) +++ ruby_1_8/object.c (revision 16150) @@ -1212,6 +1212,35 @@ } +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 @@ 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); Index: ruby_1_8/test/ruby/test_symbol.rb =================================================================== --- ruby_1_8/test/ruby/test_symbol.rb (revision 16149) +++ ruby_1_8/test/ruby/test_symbol.rb (revision 16150) @@ -74,4 +74,11 @@ 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 Index: ruby_1_8/eval.c =================================================================== --- ruby_1_8/eval.c (revision 16149) +++ ruby_1_8/eval.c (revision 16150) @@ -9636,6 +9636,7 @@ Data_Get_Struct(proc, struct BLOCK, data); data->body->nd_state = YIELD_FUNC_AVALUE; + data->flags |= BLOCK_LAMBDA; return proc; } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/