From 28d8bf902d573236ee0bdfdc84de5d96deffefee Mon Sep 17 00:00:00 2001 From: marcandre Date: Tue, 6 Nov 2012 17:14:46 +0000 Subject: [PATCH] * 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 --- range.c | 22 +++++++++++++++++++++- test/ruby/test_range.rb | 8 ++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/range.c b/range.c index c5e0d926de..b449e97758 100644 --- a/range.c +++ b/range.c @@ -456,6 +456,25 @@ sym_each_i(VALUE v, void *arg) 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: * rng.each {| i | block } -> rng @@ -482,7 +501,7 @@ range_each(VALUE range) { VALUE beg, end; - RETURN_ENUMERATOR(range, 0, 0); + RETURN_SIZED_ENUMERATOR(range, 0, 0, range_size); beg = RANGE_BEG(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, "min", range_min, 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, "inspect", range_inspect, 0); diff --git a/test/ruby/test_range.rb b/test/ruby/test_range.rb index 738c7cdb85..2827bc1396 100644 --- a/test/ruby/test_range.rb +++ b/test/ruby/test_range.rb @@ -347,4 +347,12 @@ class TestRange < Test::Unit::TestCase assert !x.eql?(z) } 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