Class: LLM::Repl::Walker
- Inherits:
-
Object
- Object
- LLM::Repl::Walker
- Defined in:
- lib/llm/repl/walker.rb
Overview
Walks through an array from the last element backward, one step at a time. #next moves forward toward the end. Both methods clamp at the array boundaries.
Instance Attribute Summary collapse
-
#cursor ⇒ Object
writeonly
Sets the cursor position.
Instance Method Summary collapse
-
#initialize(items) ⇒ Walker
constructor
A new instance of Walker.
- #next ⇒ Object?
- #prev ⇒ Object?
Constructor Details
#initialize(items) ⇒ Walker
Returns a new instance of Walker.
15 16 17 18 |
# File 'lib/llm/repl/walker.rb', line 15 def initialize(items) @items = items @cursor = items.size end |
Instance Attribute Details
#cursor=(value) ⇒ Object (writeonly)
Sets the cursor position
11 12 13 |
# File 'lib/llm/repl/walker.rb', line 11 def cursor=(value) @cursor = value end |
Instance Method Details
#next ⇒ Object?
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/llm/repl/walker.rb', line 22 def next if @items.empty? nil elsif @cursor >= @items.size - 1 @items[@cursor] else @cursor += 1 @items[@cursor] end end |
#prev ⇒ Object?
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/llm/repl/walker.rb', line 35 def prev if @items.empty? nil elsif @cursor <= 0 @items[@cursor] else @cursor -= 1 @items[@cursor] end end |