Class: LLM::Repl::Walker

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(items) ⇒ Walker

Returns a new instance of Walker.

Parameters:

  • items (Array)


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

#nextObject?

Returns:



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

#prevObject?

Returns:



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