lib/core/facets/array/indexable.rb in facets-2.1.0 vs lib/core/facets/array/indexable.rb in facets-2.1.1
- old
+ new
@@ -26,9 +26,45 @@
# a = [1,2]
# a.last! 3
# p a #=> [1,2,3]
#
alias_method :last!, :pop
+
+ # Iteration object.
+
+ class It
+ attr_reader :index, :value, :prior, :after
+ def initialize(array)
+ @array = array
+ @index = 0
+ @value = array[0]
+ @prior = []
+ @after = array[1..-1]
+ end
+ def first? ; @index == 0 ; end
+ def last? ; @index == @array.length ; end
+ private
+ def next_iteration
+ @index += 1
+ @prior << @value
+ @value = @after.shift
+ end
+ end
+
+ # Iterate over each element of array using an iteration object.
+
+ def each_iteration
+ if block_given?
+ it = It.new(self)
+ each do |e|
+ yield(it)
+ it.send(:next_iteration)
+ end
+ else
+ return Enumerable::Enumerator.new(self, :each_iteration)
+ end
+ end
+
end
=begin
class Array