Project

General

Profile

Actions

Bug #8815

closed

Enumerable.drop_while returns an Enumerator. Calling next twice on the Enumerator raises StopIteration even if there are still items available

Added by ebouchut (Eric Bouchut) over 10 years ago. Updated over 10 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.3.0]
[ruby-core:56789]

Description

I have an enumerable (array) that contains 3 items
[1, 2, 3].
I send drop_while without a block to this array to get an Enumerator.
enumerator = [1, 2, 3].drop_while

When I call next twice on the Enumerator
enumerator.next # => 1
enumerator.next # => raises StopIteration
The second call enumerator.next raises StopIteration whereas the Enumerable contains more than one item.

I expected the second call to enumerator.next to return 2 instead.

I reproduced this on:

  • ruby-1.9.3-p448
  • ruby-2.0.0-p247
  • rbx-2.0.0
    However it works fine in 1.8.7, even if 1.8.7's documentation did not mention about Enumerable.drop_while returning an Enumerator.

Here is an RSpec example that isolates the problem:
http://preview.tinyurl.com/enumerable-drop-while

Eric

Updated by nobu (Nobuyoshi Nakada) over 10 years ago

  • Status changed from Open to Rejected

Not a bug.

Enumerable#drop_while stops if the block returns false (or nil).
You have to feed true value to the enumerator by Enumerator#feed.

enumerator.next # => 1
enumerator.feed(true)
enumerator.next # => 2

Updated by JEG2 (James Gray) over 10 years ago

Isn't that backwards?

>> [1, 2, 3].drop_while { nil }
=> [1, 2, 3]
>> [1, 2, 3].drop_while { false }
=> [1, 2, 3]
>> [1, 2, 3].drop_while { true }
=> []
Actions

Also available in: Atom PDF

Like0
Like0Like0