mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* range.c (range_max): treat end exclusion without iteration if
the end value is an integer. [ruby-talk:167433] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9613 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
689cc98238
commit
9c5a4caf72
3 changed files with 37 additions and 6 deletions
|
@ -1,3 +1,8 @@
|
|||
Fri Nov 25 14:34:09 2005 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||
|
||||
* range.c (range_max): treat end exclusion without iteration if
|
||||
the end value is an integer. [ruby-talk:167433]
|
||||
|
||||
Fri Nov 25 12:52:57 2005 Kouhei Sutou <kou@cozmixng.org>
|
||||
|
||||
* lib/rss/rss.rb: added backward compatibility codes.
|
||||
|
|
18
range.c
18
range.c
|
@ -457,8 +457,7 @@ range_min(VALUE range)
|
|||
else {
|
||||
VALUE b = rb_ivar_get(range, id_beg);
|
||||
VALUE e = rb_ivar_get(range, id_end);
|
||||
VALUE r = rb_funcall(b, id_cmp, 1, e);
|
||||
int c = rb_cmpint(r, b, e);
|
||||
int c = rb_cmpint(rb_funcall(b, id_cmp, 1, e), b, e);
|
||||
|
||||
if (c > 0) return Qnil;
|
||||
return b;
|
||||
|
@ -480,16 +479,23 @@ range_min(VALUE range)
|
|||
static VALUE
|
||||
range_max(VALUE range)
|
||||
{
|
||||
if (rb_block_given_p() || EXCL(range)) {
|
||||
VALUE e = rb_ivar_get(range, id_end);
|
||||
int ip = FIXNUM_P(e) || rb_obj_is_kind_of(e, rb_cInteger);
|
||||
|
||||
if (rb_block_given_p() || (EXCL(range) && !ip)) {
|
||||
return rb_call_super(0, 0);
|
||||
}
|
||||
else {
|
||||
VALUE b = rb_ivar_get(range, id_beg);
|
||||
VALUE e = rb_ivar_get(range, id_end);
|
||||
VALUE r = rb_funcall(b, id_cmp, 1, e);
|
||||
int c = rb_cmpint(r, b, e);
|
||||
int c = rb_cmpint(rb_funcall(b, id_cmp, 1, e), b, e);
|
||||
|
||||
if (c > 0) return Qnil;
|
||||
if (EXCL(range)) {
|
||||
if (FIXNUM_P(e)) {
|
||||
return INT2NUM(FIX2INT(e)-1);
|
||||
}
|
||||
return rb_funcall(e, '-', 1, INT2FIX(1));
|
||||
}
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,4 +31,24 @@ class TestRange < Test::Unit::TestCase
|
|||
def test_duckrange
|
||||
assert_equal("bc", "abcd"[DuckRange.new(1,2)])
|
||||
end
|
||||
|
||||
def test_min
|
||||
assert_equal(1, (1..2).min)
|
||||
assert_equal(nil, (2..1).min)
|
||||
assert_equal(1, (1...2).min)
|
||||
|
||||
assert_equal(1.0, (1.0..2.0).min)
|
||||
assert_equal(nil, (2.0..1.0).min)
|
||||
assert_equal(1, (1.0...2.0).min)
|
||||
end
|
||||
|
||||
def test_max
|
||||
assert_equal(2, (1..2).max)
|
||||
assert_equal(nil, (2..1).max)
|
||||
assert_equal(1, (1...2).max)
|
||||
|
||||
assert_equal(2.0, (1.0..2.0).max)
|
||||
assert_equal(nil, (2.0..1.0).max)
|
||||
assert_raise(TypeError) { (1.0...2.0).max }
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue