summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-11-06 17:13:04 +0000
committermarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-11-06 17:13:04 +0000
commitfe9386cdcbfce8c631a333add2b2cab8366ee6dc (patch)
treed85cb7ff829beafa05868d87b8e6b884d208e419
parentdf8451e6062029cb23593679317be97d1c218c7b (diff)
* enum.c (enum_each_cons): Support for Enumerable#each_cons.size
[Feature #6636] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37509 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--enum.c16
-rw-r--r--test/ruby/test_enumerator.rb7
2 files changed, 22 insertions, 1 deletions
diff --git a/enum.c b/enum.c
index 2f636ed07b..75b18454c9 100644
--- a/enum.c
+++ b/enum.c
@@ -1849,6 +1849,20 @@ each_cons_i(VALUE i, VALUE args, int argc, VALUE *argv)
return v;
}
+static VALUE
+enum_each_cons_size(VALUE obj, VALUE args)
+{
+ VALUE n, size;
+ long cons_size = NUM2LONG(RARRAY_PTR(args)[0]);
+ if (cons_size <= 0) rb_raise(rb_eArgError, "invalid size");
+
+ size = enum_size(obj, 0);
+ if (size == Qnil) return Qnil;
+
+ n = rb_funcall(size, '+', 1, LONG2NUM(1 - cons_size));
+ return (rb_cmpint(rb_funcall(n, id_cmp, 1, LONG2FIX(0)), n, LONG2FIX(0)) == -1) ? LONG2FIX(0) : n;
+}
+
/*
* call-seq:
* enum.each_cons(n) { ... } -> nil
@@ -1877,7 +1891,7 @@ enum_each_cons(VALUE obj, VALUE n)
NODE *memo;
if (size <= 0) rb_raise(rb_eArgError, "invalid size");
- RETURN_ENUMERATOR(obj, 1, &n);
+ RETURN_SIZED_ENUMERATOR(obj, 1, &n, enum_each_cons_size);
memo = NEW_MEMO(rb_ary_new2(size), 0, size);
rb_block_call(obj, id_each, 0, 0, each_cons_i, (VALUE)memo);
diff --git a/test/ruby/test_enumerator.rb b/test/ruby/test_enumerator.rb
index 1e3eb7c8b8..a8564c1ce0 100644
--- a/test/ruby/test_enumerator.rb
+++ b/test/ruby/test_enumerator.rb
@@ -490,5 +490,12 @@ class TestEnumerator < Test::Unit::TestCase
assert_equal 1, @sized.each_slice(70).size
assert_raise(ArgumentError){ @obj.each_slice(0).size }
end
+
+ def test_size_for_each_cons
+ assert_equal nil, @obj.each_cons(3).size
+ assert_equal 33, @sized.each_cons(10).size
+ assert_equal 0, @sized.each_cons(70).size
+ assert_raise(ArgumentError){ @obj.each_cons(0).size }
+ end
end