Class: Twig::Runtime::LoopIterator
- Inherits:
-
Object
- Object
- Twig::Runtime::LoopIterator
- Defined in:
- lib/twig/runtime/loop_iterator.rb
Instance Attribute Summary collapse
-
#index0 ⇒ Object
readonly
Returns the value of attribute index0.
-
#next ⇒ Object
readonly
Returns the value of attribute next.
-
#previous ⇒ Object
readonly
Returns the value of attribute previous.
Instance Method Summary collapse
- #each ⇒ Object
- #first ⇒ Object
- #index ⇒ Object
-
#initialize(seq) ⇒ LoopIterator
constructor
A new instance of LoopIterator.
- #last ⇒ Object
- #length ⇒ Object
- #revindex ⇒ Object
- #revindex0 ⇒ Object
Constructor Details
#initialize(seq) ⇒ LoopIterator
Returns a new instance of LoopIterator.
8 9 10 11 12 13 |
# File 'lib/twig/runtime/loop_iterator.rb', line 8 def initialize(seq) @seq = Runtime::EnumerableHash.from(seq).to_enum @index0 = 0 @previous = nil @next = nil end |
Instance Attribute Details
#index0 ⇒ Object (readonly)
Returns the value of attribute index0.
6 7 8 |
# File 'lib/twig/runtime/loop_iterator.rb', line 6 def index0 @index0 end |
#next ⇒ Object (readonly)
Returns the value of attribute next.
6 7 8 |
# File 'lib/twig/runtime/loop_iterator.rb', line 6 def next @next end |
#previous ⇒ Object (readonly)
Returns the value of attribute previous.
6 7 8 |
# File 'lib/twig/runtime/loop_iterator.rb', line 6 def previous @previous end |
Instance Method Details
#each ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/twig/runtime/loop_iterator.rb', line 15 def each(&) @index0 = 0 loop do current = @seq.next begin @next = @seq.peek rescue StopIteration @next = nil end yield current[0], current[1] @index0 += 1 @previous = current rescue StopIteration break end end |
#first ⇒ Object
35 36 37 |
# File 'lib/twig/runtime/loop_iterator.rb', line 35 def first @index0.zero? end |
#index ⇒ Object
47 48 49 |
# File 'lib/twig/runtime/loop_iterator.rb', line 47 def index @index0 + 1 end |
#last ⇒ Object
39 40 41 |
# File 'lib/twig/runtime/loop_iterator.rb', line 39 def last revindex0.zero? || length.zero? end |
#length ⇒ Object
43 44 45 |
# File 'lib/twig/runtime/loop_iterator.rb', line 43 def length @length ||= @seq.count end |
#revindex ⇒ Object
55 56 57 |
# File 'lib/twig/runtime/loop_iterator.rb', line 55 def revindex revindex0 + 1 end |
#revindex0 ⇒ Object
51 52 53 |
# File 'lib/twig/runtime/loop_iterator.rb', line 51 def revindex0 [0, length - index].max end |