Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/iteration.rb

Instance Method Summary collapse

Instance Method Details

#each_iteration(&block) ⇒ Enumerator

Iterate over each element of array using an iteration object.

[1,2,3].each_iteration do |it|
  p it.index
  p it.value
  p it.first?
  p it.last?
  p it.prior
  p it.after
end

On each successive iteration this produces:

0          1          2
1          2          3
true       false      false
false      false      true
[]         [1]        [1,2]
[2,3]      [3]        []

Returns:

  • (Enumerator)

    if no block is given, otherwise nothing.



96
97
98
99
100
101
102
103
104
105
# File 'lib/iteration.rb', line 96

def each_iteration(&block)
  if block_given?
    it = Iteration.new(self)
    each do |e|
      it.__step__(e){ yield(it) }
    end
  else
    Enumerator.new(self, :each_iteration)
  end
end

#each_with_iteration(&block) ⇒ Enumerator

Same as #each_iteration, but provides both the iterated element and the iteration.

Returns:

  • (Enumerator)

    if no block is given, otherwise nothing.



111
112
113
114
115
116
117
118
119
120
# File 'lib/iteration.rb', line 111

def each_with_iteration(&block)
  if block_given?
    it = Iteration.new(self)
    each do |e|
      it.__step__(e){ yield(e, it) }
    end
  else
    Enumerator.new(self, :each_with_iteration)
  end
end