range.c (range_last): disable optimization when each is redefined

Do not use the optimized version of Range#last when Range#each is
redefined.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66749 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mrkn 2019-01-08 04:37:40 +00:00
parent 8371a9a4ce
commit 2b9c6e1a8a
2 changed files with 14 additions and 1 deletions

View File

@ -1088,7 +1088,8 @@ range_last(int argc, VALUE *argv, VALUE range)
b = RANGE_BEG(range);
e = RANGE_END(range);
if (RB_INTEGER_TYPE_P(b) && RB_INTEGER_TYPE_P(e)) {
if (RB_INTEGER_TYPE_P(b) && RB_INTEGER_TYPE_P(e) &&
RB_LIKELY(rb_method_basic_definition_p(rb_cRange, idEach))) {
return rb_int_range_last(argc, argv, range);
}
return rb_ary_last(argc, argv, rb_Array(range));

View File

@ -450,6 +450,18 @@ class TestRange < Test::Unit::TestCase
assert_raise(ArgumentError) { (0..10).last(-1) }
end
def test_last_with_redefine_each
assert_in_out_err([], <<-'end;', ['true'], [])
class Range
remove_method :each
def each(&b)
[1, 2, 3, 4, 5].each(&b)
end
end
puts [3, 4, 5] == (1..10).last(3)
end;
end
def test_to_s
assert_equal("0..1", (0..1).to_s)
assert_equal("0...1", (0...1).to_s)