class Soroban::ValueWalker

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

Public Class Methods

new(range, context) click to toggle source

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

# File lib/soroban/value_walker.rb, line 13
def initialize(range, context)
  @_range, @_binding = range, context
  @_labels = Soroban::LabelWalker.new(range).to_a
end

Public Instance Methods

[](index) click to toggle source

Get the value of a cell within the range by index. Will raise a RangeError if the index is outside of the range.

# File lib/soroban/value_walker.rb, line 25
def [](index)
  if index < 0 || index >= @_labels.length
    raise Soroban::RangeError, "Index #{index} falls outside of '#{@_range}'"
  end
  eval("get('#{@_labels[index]}')", @_binding)
end
[]=(index, value) click to toggle source

Set the value of a cell within the range by index. Will raise a RangeError if the index is outside of the range.

# File lib/soroban/value_walker.rb, line 34
def []=(index, value)
  if index < 0 || index >= @_labels.length
    raise Soroban::RangeError, "Index #{index} falls outside of '#{@_range}'"
  end
  eval("@#{@_labels[index]}.set('#{value}')", @_binding)
  return value
end
each() { |eval("get('#{label}')", _binding)| ... } click to toggle source

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

# File lib/soroban/value_walker.rb, line 19
def each
  @_labels.each { |label| yield eval("get('#{label}')", @_binding) }
end
inspect()
Alias for: to_s
to_s() click to toggle source

Display the range if the user outputs the binding directly

# File lib/soroban/value_walker.rb, line 43
def to_s
  @_range
end
Also aliased as: inspect