diff options
-rw-r--r-- | enumerator.c | 15 | ||||
-rw-r--r-- | test/ruby/test_lazy_enumerator.rb | 5 |
2 files changed, 19 insertions, 1 deletions
diff --git a/enumerator.c b/enumerator.c index e5dd80a841..0a51709ac1 100644 --- a/enumerator.c +++ b/enumerator.c @@ -1674,6 +1674,19 @@ lazy_take_while(VALUE obj) } static VALUE +lazy_drop_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)) + return receiver; + if (FIXNUM_P(receiver)) { + len = FIX2LONG(receiver) - len; + return LONG2FIX(len < 0 ? 0 : len); + } + return rb_funcall(receiver, '-', 1, LONG2NUM(len)); +} + +static VALUE lazy_drop_func(VALUE val, VALUE args, int argc, VALUE *argv) { NODE *memo = RNODE(args); @@ -1699,7 +1712,7 @@ lazy_drop(VALUE obj, VALUE n) memo = NEW_MEMO(0, 0, len); return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj, lazy_drop_func, (VALUE) memo), - rb_ary_new3(1, n), 0); + rb_ary_new3(1, n), lazy_drop_size); } static VALUE diff --git a/test/ruby/test_lazy_enumerator.rb b/test/ruby/test_lazy_enumerator.rb index e09c3af837..9647590136 100644 --- a/test/ruby/test_lazy_enumerator.rb +++ b/test/ruby/test_lazy_enumerator.rb @@ -341,5 +341,10 @@ EOS assert_equal 3, lazy.take(4).size assert_equal 4, loop.lazy.take(4).size assert_equal nil, lazy.select{}.take(4).size + + assert_equal 1, lazy.drop(2).size + assert_equal 0, lazy.drop(4).size + assert_equal Float::INFINITY, loop.lazy.drop(4).size + assert_equal nil, lazy.select{}.drop(4).size end end |