Class: Basic101::OnGotoStatement

Inherits:
Statement show all
Defined in:
lib/basic101/on_goto_statement.rb

Instance Attribute Summary collapse

Attributes inherited from Statement

#index, #line

Instance Method Summary collapse

Methods inherited from Statement

#data_items, #exec, #line_number, #raise_error_with_line_number

Methods included from Identity

#==

Constructor Details

#initialize(expression, line_numbers) ⇒ OnGotoStatement

Returns a new instance of OnGotoStatement.



11
12
13
14
# File 'lib/basic101/on_goto_statement.rb', line 11

def initialize(expression, line_numbers)
  @expression = expression
  @line_numbers = line_numbers
end

Instance Attribute Details

#next_statement=(value) ⇒ Object (writeonly)

Sets the attribute next_statement

Parameters:

  • value

    the value to set the attribute next_statement to.



9
10
11
# File 'lib/basic101/on_goto_statement.rb', line 9

def next_statement=(value)
  @next_statement = value
end

Instance Method Details

#execute(runtime) ⇒ Object



16
17
18
19
20
21
# File 'lib/basic101/on_goto_statement.rb', line 16

def execute(runtime)
  index = @expression.eval(runtime).to_i - 1
  if (0...@line_numbers.size).include?(index)
    runtime.goto_line(@line_numbers[index].to_i)
  end
end