Class: Enumerator

Inherits:
Object
  • Object
show all
Defined in:
lib/b-lazy.rb

Overview

We really don’t need to make any significant changes to the Enumerator class itself. We’re just adding a few convenient methods that can simplify looping constructs.

Instance Method Summary collapse

Instance Method Details

#empty?Boolean

If the Enumerator is empty, then return true. Otherwise, return false.

Returns:

  • (Boolean)


20
21
22
23
24
25
# File 'lib/b-lazy.rb', line 20

def empty?
  peek
  false
rescue StopIteration
  true
end

#grab(n) ⇒ Object

This is similar to the #take method, except that it actually moves the Enumerator ahead n after each call.



30
31
32
33
34
35
36
37
# File 'lib/b-lazy.rb', line 30

def grab(n)
  retval = []
  begin
    n.times{retval << self.next}
  rescue StopIteration
  end
  retval
end

#has_next?Boolean

If the Enumerator is empty, then return false. Otherwise, return true.

Returns:

  • (Boolean)


10
11
12
13
14
15
# File 'lib/b-lazy.rb', line 10

def has_next?
  peek
  true
rescue StopIteration
  false
end