Class: Basic101::IfStatement

Inherits:
Statement show all
Defined in:
lib/basic101/if_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(condition) ⇒ IfStatement

Returns a new instance of IfStatement.



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

def initialize(condition)
  @condition = condition
  @true_statement = nil
end

Instance Attribute Details

#else_statement=(value) ⇒ Object (writeonly)

Sets the attribute else_statement

Parameters:

  • value

    the value to set the attribute else_statement to.



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

def else_statement=(value)
  @else_statement = value
end

Instance Method Details

#end_statement=(statement) ⇒ Object



16
17
18
# File 'lib/basic101/if_statement.rb', line 16

def end_statement=(statement)
  @else_statement.end_statement = statement
end

#execute(runtime) ⇒ Object



20
21
22
23
24
# File 'lib/basic101/if_statement.rb', line 20

def execute(runtime)
  if @condition.eval(runtime).to_f == 0
    @else_statement.goto_following_statement(runtime)
  end
end