* range.c: Support for Range#size and Range#each.size

[Feature #6636]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37516 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
marcandre 2012-11-06 17:14:46 +00:00
Родитель 34be3a5d90
Коммит 28d8bf902d
2 изменённых файлов: 29 добавлений и 1 удалений

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

@ -456,6 +456,25 @@ sym_each_i(VALUE v, void *arg)
return Qnil; return Qnil;
} }
/*
* call-seq:
* rng.size -> num
*
* Returns the number of elements in the range.
*
* (10..20).size #=> 11
*/
static VALUE
range_size(VALUE range)
{
VALUE b = RANGE_BEG(range), e = RANGE_END(range);
if (rb_obj_is_kind_of(b, rb_cNumeric) && rb_obj_is_kind_of(e, rb_cNumeric)) {
return num_interval_step_size(b, e, INT2FIX(1), EXCL(range));
}
return Qnil;
}
/* /*
* call-seq: * call-seq:
* rng.each {| i | block } -> rng * rng.each {| i | block } -> rng
@ -482,7 +501,7 @@ range_each(VALUE range)
{ {
VALUE beg, end; VALUE beg, end;
RETURN_ENUMERATOR(range, 0, 0); RETURN_SIZED_ENUMERATOR(range, 0, 0, range_size);
beg = RANGE_BEG(range); beg = RANGE_BEG(range);
end = RANGE_END(range); end = RANGE_END(range);
@ -1076,6 +1095,7 @@ Init_Range(void)
rb_define_method(rb_cRange, "last", range_last, -1); rb_define_method(rb_cRange, "last", range_last, -1);
rb_define_method(rb_cRange, "min", range_min, 0); rb_define_method(rb_cRange, "min", range_min, 0);
rb_define_method(rb_cRange, "max", range_max, 0); rb_define_method(rb_cRange, "max", range_max, 0);
rb_define_method(rb_cRange, "size", range_size, 0);
rb_define_method(rb_cRange, "to_s", range_to_s, 0); rb_define_method(rb_cRange, "to_s", range_to_s, 0);
rb_define_method(rb_cRange, "inspect", range_inspect, 0); rb_define_method(rb_cRange, "inspect", range_inspect, 0);

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

@ -347,4 +347,12 @@ class TestRange < Test::Unit::TestCase
assert !x.eql?(z) assert !x.eql?(z)
} }
end end
def test_size
assert_equal 42, (1..42).size
assert_equal 41, (1...42).size
assert_equal 6, (1...6.3).size
assert_equal 5, (1.1...6).size
assert_equal 42, (1..42).each.size
end
end end