Class: BlocklyInterpreter::CoreBlocks::ListsGetIndexBlock

Inherits:
Block
  • Object
show all
Includes:
DSLGenerator
Defined in:
lib/blockly_interpreter/core_blocks/lists_get_index_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

Constructor Details

This class inherits a constructor from BlocklyInterpreter::Block

Instance Method Details

#execute_statement(execution_context) ⇒ Object



9
10
11
12
# File 'lib/blockly_interpreter/core_blocks/lists_get_index_block.rb', line 9

def execute_statement(execution_context)
  return unless is_statement?
  value(execution_context)
end

#is_statement?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/blockly_interpreter/core_blocks/lists_get_index_block.rb', line 5

def is_statement?
  mutation.try(:[], 'statement') == 'true'
end

#to_dslObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/blockly_interpreter/core_blocks/lists_get_index_block.rb', line 25

def to_dsl
  case fields['WHERE']
  when 'FIRST', 'LAST', 'RANDOM'
    method_call_with_possible_block("lists_get_#{fields['WHERE'].downcase}", "", values['VALUE'])
  else
    block_contents = [
      method_call_with_block_or_nothing('at', '', values['AT']),
      method_call_with_block_or_nothing('list', '', values['VALUE'])
    ].compact

    method_call_with_possible_block("lists_get_index", "mode: #{fields["MODE"].inspect}, where: #{fields["WHERE"].inspect}", block_contents)
  end
end

#value(execution_context) ⇒ Object



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

def value(execution_context)
  list = values['VALUE'].value(execution_context)
  return unless list

  list_index = index(execution_context, list)

  value_at_index = list[list_index]
  list.delete_at(list_index) if %w(GET_REMOVE REMOVE).include?(fields['MODE'])
  value_at_index
end