Class: CodeDiscrepancyInstruction

Inherits:
Instruction show all
Defined in:
lib/instructions/code/code_discrepancy.rb

Instance Attribute Summary

Attributes inherited from Instruction

#context

Instance Method Summary collapse

Methods inherited from Instruction

all_instructions, #go, inherited, #initialize, #needs, #pushes, to_nudgecode

Constructor Details

This class inherits a constructor from Instruction

Instance Method Details

#cleanupObject



26
27
28
# File 'lib/instructions/code/code_discrepancy.rb', line 26

def cleanup
  pushes :int, @result
end

#deriveObject



17
18
19
20
21
22
23
24
25
# File 'lib/instructions/code/code_discrepancy.rb', line 17

def derive
   # collect string blueprints of every point
  parts_of_1 = NudgeProgram.new(@arg1).linked_code.collect {|pt| pt.blueprint}
  parts_of_2 = NudgeProgram.new(@arg2).linked_code.collect {|pt| pt.blueprint}
  unique_parts = (parts_of_1.uniq + parts_of_2.uniq).reject {|i| i == ""}
  summed_differences = unique_parts.inject(0) {|sum, uniq_string| sum +
    (parts_of_2.count(uniq_string) - parts_of_1.count(uniq_string)).abs}
  @result = ValuePoint.new("int", summed_differences)
end

#preconditions?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/instructions/code/code_discrepancy.rb', line 10

def preconditions?
  needs :code, 2
end

#setupObject



13
14
15
16
# File 'lib/instructions/code/code_discrepancy.rb', line 13

def setup
  @arg2 = @context.pop_value(:code)
  @arg1 = @context.pop_value(:code)
end