ruby-changes:12839
From: matz <ko1@a...>
Date: Tue, 18 Aug 2009 01:41:18 +0900 (JST)
Subject: [ruby-changes:12839] Ruby:r24571 (trunk): * range.c (range_each): should honor to_str conversion.
matz 2009-08-18 01:41:00 +0900 (Tue, 18 Aug 2009) New Revision: 24571 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=24571 Log: * range.c (range_each): should honor to_str conversion. Modified files: trunk/ChangeLog trunk/range.c trunk/test/ruby/test_range.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 24570) +++ ChangeLog (revision 24571) @@ -1,3 +1,7 @@ +Tue Aug 18 01:21:31 2009 Yukihiro Matsumoto <matz@r...> + + * range.c (range_each): should honor to_str conversion. + Mon Aug 17 23:45:40 2009 Tadayoshi Funaba <tadf@d...> * lib/date/delta.rb: removed require 'date'. added to_c. Index: range.c =================================================================== --- range.c (revision 24570) +++ range.c (revision 24571) @@ -436,19 +436,23 @@ rb_yield(LONG2FIX(i)); } } - else if (TYPE(beg) == T_STRING) { - VALUE args[2]; + else { + VALUE tmp = rb_check_string_type(beg); - args[0] = end; - args[1] = EXCL(range) ? Qtrue : Qfalse; - rb_block_call(beg, rb_intern("upto"), 2, args, rb_yield, 0); - } - else { - if (!rb_respond_to(beg, id_succ)) { - rb_raise(rb_eTypeError, "can't iterate from %s", - rb_obj_classname(beg)); + if (!NIL_P(tmp)) { + VALUE args[2]; + + args[0] = end; + args[1] = EXCL(range) ? Qtrue : Qfalse; + rb_block_call(tmp, rb_intern("upto"), 2, args, rb_yield, 0); } - range_each_func(range, each_i, NULL); + else { + if (!rb_respond_to(beg, id_succ)) { + rb_raise(rb_eTypeError, "can't iterate from %s", + rb_obj_classname(beg)); + } + range_each_func(range, each_i, NULL); + } } return range; } Index: test/ruby/test_range.rb =================================================================== --- test/ruby/test_range.rb (revision 24570) +++ test/ruby/test_range.rb (revision 24571) @@ -1,4 +1,5 @@ require 'test/unit' +require 'delegate' class TestRange < Test::Unit::TestCase def test_range_string @@ -13,6 +14,8 @@ assert_equal(["6", "7", "8"], ("6".."8").to_a, "[ruby-talk:343187]") assert_equal(["6", "7"], ("6"..."8").to_a) assert_equal(["9", "10"], ("9".."10").to_a) + assert_equal(["9", "10"], (SimpleDelegator.new("9").."10").to_a) + assert_equal(["9", "10"], ("9"..SimpleDelegator.new("10")).to_a) end def test_evaluation_order -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/