summaryrefslogtreecommitdiff
path: root/enumerator.c
diff options
context:
space:
mode:
authormarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-11-06 17:16:29 +0000
committermarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-11-06 17:16:29 +0000
commit9aafa954aa9ddb54f943c029100c03c4d4a1701b (patch)
tree33ea5c8b0e25107b4bb4f80934eaa575363138c3 /enumerator.c
parent5dbbfc3b3463d08b290e33975d815b6336069810 (diff)
* enumerator.c: Support for lazy.take.size
[Feature #6636] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37523 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'enumerator.c')
-rw-r--r--enumerator.c11
1 files changed, 10 insertions, 1 deletions
diff --git a/enumerator.c b/enumerator.c
index 1324c9284c..e5dd80a841 100644
--- a/enumerator.c
+++ b/enumerator.c
@@ -1625,6 +1625,15 @@ lazy_take_func(VALUE val, VALUE args, int argc, VALUE *argv)
}
static VALUE
+lazy_take_size(VALUE lazy) {
+ long len = NUM2LONG(RARRAY_PTR(rb_ivar_get(lazy, id_arguments))[0]);
+ VALUE receiver = lazy_receiver_size(lazy);
+ if (NIL_P(receiver) || (FIXNUM_P(receiver) && FIX2LONG(receiver) < len))
+ return receiver;
+ return LONG2NUM(len);
+}
+
+static VALUE
lazy_take(VALUE obj, VALUE n)
{
NODE *memo;
@@ -1644,7 +1653,7 @@ lazy_take(VALUE obj, VALUE n)
memo = NEW_MEMO(0, len, len);
return lazy_set_method(rb_block_call(rb_cLazy, id_new, argc, argv,
lazy_take_func, (VALUE) memo),
- rb_ary_new3(1, n), 0);
+ rb_ary_new3(1, n), lazy_take_size);
}
static VALUE