Class: Iteration

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

Overview

Iteration encapsulates a step in an each loop.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enum) ⇒ Iteration

Returns a new instance of Iteration.



12
13
14
15
16
17
# File 'lib/iteration.rb', line 12

def initialize(enum)
  @enum  = enum
  @index = 0
  @value = nil
  @prior = []
end

Instance Attribute Details

#enumObject (readonly)

Returns the value of attribute enum.



10
11
12
# File 'lib/iteration.rb', line 10

def enum
  @enum
end

#indexObject (readonly)

Returns the value of attribute index.



10
11
12
# File 'lib/iteration.rb', line 10

def index
  @index
end

#priorObject (readonly)

Returns the value of attribute prior.



10
11
12
# File 'lib/iteration.rb', line 10

def prior
  @prior
end

#valueObject (readonly)

Returns the value of attribute value.



10
11
12
# File 'lib/iteration.rb', line 10

def value
  @value
end

Instance Method Details

#__step__(value, &block) ⇒ Object

TODO: For Ruby 1.9 make private and use fcall ?



35
36
37
38
39
40
# File 'lib/iteration.rb', line 35

def __step__(value, &block)
  @value = value
  block.call
  @index += 1
  @prior << value
end

#afterObject



27
28
29
# File 'lib/iteration.rb', line 27

def after
  enum.slice(index+1..-1)
end

#first?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/iteration.rb', line 19

def first?
  index == 0
end

#last?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/iteration.rb', line 23

def last?
  index+1 == enum.size
end