Class: Soroban::ValueWalker

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/soroban/value_walker.rb

Overview

An enumerable that allows cells in a range to be visited.

Instance Method Summary (collapse)

Constructor Details

- (ValueWalker) initialize(range, context)

Create a new walker from a supplied range and binding. The binding is required when calculating the value of each visited cell.



10
11
12
13
# File 'lib/soroban/value_walker.rb', line 10

def initialize(range, context)
  @range, @binding = range, context
  @walker = LabelWalker.new(range)
end

Instance Method Details

- (Object) [](index)

Retrieve the value of a cell within the range by index



21
22
23
24
25
26
27
# File 'lib/soroban/value_walker.rb', line 21

def [](index)
  labels = @walker.to_a
  if index < 0 || index >= labels.length
    raise Soroban::RangeError, "Index #{index} falls outside of '#{@range}'"
  end
  eval("get('#{labels[index]}')", @binding)
end

- (Object) []=(index, value)

Set the value of a cell within the range by index



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/soroban/value_walker.rb', line 30

def []=(index, value)
  count = 0
  @walker.each do |label|
    if index == count
      eval("@#{label}.set('#{value}')", @binding)
      return value
    end
    count += 1
  end
  raise Soroban::RangeError, "Index #{index} falls outside of '#{@range}'"
end

- (Object) each

Yield the value of each cell referenced by the supplied range.



16
17
18
# File 'lib/soroban/value_walker.rb', line 16

def each
  @walker.each { |label| yield eval("get('#{label}')", @binding) }
end

- (Object) to_s Also known as: inspect

Display the range if the user outputs the binding directly



43
44
45
# File 'lib/soroban/value_walker.rb', line 43

def to_s
  @range
end