Class: BlocklyInterpreter::CoreBlocks::ForBlock

Inherits:
Block
  • Object
show all
Includes:
DSLGenerator
Defined in:
lib/blockly_interpreter/core_blocks/for_block.rb

Defined Under Namespace

Modules: DSLMethods

Instance Attribute Summary

Attributes inherited from Block

#block_type, #comment, #comment_pinned, #fields, #is_shadow, #mutation, #next_block, #statements, #values, #x, #y

Instance Method Summary collapse

Methods included from DSLGenerator

#deep_flatten, #formatted_keyword_args, #indent, #keyword_args_without_defaults, #method_call, #method_call_with_block_or_nothing, #method_call_with_possible_block, #start_block_to_dsl, #strip_trailing_whitespace, #timestamp_to_dsl

Methods inherited from Block

#each_block, #has_comment?, #has_position?, #initialize, #to_xml, #to_xml_element, #value

Constructor Details

This class inherits a constructor from BlocklyInterpreter::Block

Instance Method Details

#execute_statement(execution_context) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/blockly_interpreter/core_blocks/for_block.rb', line 5

def execute_statement(execution_context)
  from = values['FROM'].value(execution_context).to_i
  to = values['TO'].value(execution_context).to_i
  by = values['BY'].value(execution_context).to_i

  from.step(by: by, to: to) do |i|
    execution_context.set_variable(fields['VAR'], i)
    execution_context.execute(statements['DO'])
  end
end

#to_dslObject



16
17
18
19
20
21
22
23
# File 'lib/blockly_interpreter/core_blocks/for_block.rb', line 16

def to_dsl
  method_call_with_possible_block('controls_for', fields['VAR'].inspect, [
    method_call_with_block_or_nothing('from', '', values['FROM']),
    method_call_with_block_or_nothing('to', '', values['TO']),
    method_call_with_block_or_nothing('by', '', values['BY']),
    method_call_with_block_or_nothing('action', '', statements['DO'])
  ])
end