* range.c (range_each): should honor to_str conversion.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24571 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2009-08-17 16:41:00 +00:00
Родитель a5aefa661e
Коммит 554786aedc
3 изменённых файлов: 22 добавлений и 11 удалений

Просмотреть файл

@ -1,3 +1,7 @@
Tue Aug 18 01:21:31 2009 Yukihiro Matsumoto <matz@ruby-lang.org>
* range.c (range_each): should honor to_str conversion.
Mon Aug 17 23:45:40 2009 Tadayoshi Funaba <tadf@dotrb.org> Mon Aug 17 23:45:40 2009 Tadayoshi Funaba <tadf@dotrb.org>
* lib/date/delta.rb: removed require 'date'. added to_c. * lib/date/delta.rb: removed require 'date'. added to_c.

26
range.c
Просмотреть файл

@ -436,19 +436,23 @@ range_each(VALUE range)
rb_yield(LONG2FIX(i)); rb_yield(LONG2FIX(i));
} }
} }
else if (TYPE(beg) == T_STRING) {
VALUE args[2];
args[0] = end;
args[1] = EXCL(range) ? Qtrue : Qfalse;
rb_block_call(beg, rb_intern("upto"), 2, args, rb_yield, 0);
}
else { else {
if (!rb_respond_to(beg, id_succ)) { VALUE tmp = rb_check_string_type(beg);
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);
}
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);
} }
range_each_func(range, each_i, NULL);
} }
return range; return range;
} }

Просмотреть файл

@ -1,4 +1,5 @@
require 'test/unit' require 'test/unit'
require 'delegate'
class TestRange < Test::Unit::TestCase class TestRange < Test::Unit::TestCase
def test_range_string def test_range_string
@ -13,6 +14,8 @@ class TestRange < Test::Unit::TestCase
assert_equal(["6", "7", "8"], ("6".."8").to_a, "[ruby-talk:343187]") assert_equal(["6", "7", "8"], ("6".."8").to_a, "[ruby-talk:343187]")
assert_equal(["6", "7"], ("6"..."8").to_a) assert_equal(["6", "7"], ("6"..."8").to_a)
assert_equal(["9", "10"], ("9".."10").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 end
def test_evaluation_order def test_evaluation_order