lib/iteration.rb in iteration-1.0.0 vs lib/iteration.rb in iteration-1.1.0

- old
+ new

@@ -1,82 +1,78 @@ if RUBY_VERSION < "1.9" require 'enumerator' + Enumerator = Enumerable::Enumerator end -module Enumerable +# Iteration encapsulates a step in an each loop. +# +class Iteration - # Iteration encapsulates a step in an each loop. - # - class Iteration + attr_reader :enum, :index, :value, :prior - attr_reader :enum, :index, :value, :prior + def initialize(enum) + @enum = enum + @index = 0 + @value = nil + @prior = [] + end - def initialize(enum) - @enum = enum - @index = 0 - @value = nil - @prior = [] - end + def first? + index == 0 + end - def first? - index == 0 - end + def last? + index+1 == enum.size + end - def last? - index+1 == enum.size - end + def after + enum.slice(index+1..-1) + end - def after - enum.slice(index+1..-1) - end +#private - #private + # TODO: For Ruby 1.9 make private and use fcall. + # + def __step__(value, &block) + @value = value + block.call + @index += 1 + @prior << value + end - # TODO: For Ruby 1.9 make private and use fcall. - # - def __step__(value, &block) - @value = value - block.call - @index += 1 - @prior << value - end + #def next_iteration + # @index += 1 + # @prior << value + # @after.shift if enum.respond_to?(:shift) + #end - #def next_iteration - # @index += 1 - # @prior << value - # @after.shift if enum.respond_to?(:shift) - #end - end #class Iteration +end - # This adds #iteration and #with_iteration methods - # to Enumerator. - # +class Enumerator + # TODO: How to access the underlying object of enumeration? # We need it to provide #size and #slice if possible. # - class Enumerator - def iteration #:yield: - it = Iteration.new(self) - each do |e| - it.__step__(e){ yield(it) } - end + # + def iteration #:yield: + it = Iteration.new(self) + each do |e| + it.__step__(e){ yield(it) } end + end - def with_iteration(&block) - it = Iteration.new(self) - each do |e| - it.__step__(e){ yield(e,it) } - end + def with_iteration(&block) + it = Iteration.new(self) + each do |e| + it.__step__(e){ yield(e,it) } end + end - end #class Enumerator - end -# -class Array #:nodoc: +class Array # Iterate over each element of array using an iteration object. # # [1,2,3].each_iteration do |it| # p it.index @@ -103,11 +99,11 @@ it = Iteration.new(self) each do |e| it.__step__(e){ yield(it) } end else - return Enumerable::Enumerator.new(self, :each_iteration) + Enumerator.new(self, :each_iteration) end end # Same as #each_iteration, but provides both the iterated # element and the iteration. @@ -117,10 +113,10 @@ it = Iteration.new(self) each do |e| it.__step__(e){ yield(e, it) } end else - return Enumerable::Enumerator.new(self, :each_iteration) + Enumerator.new(self, :each_iteration) end end end