1. Steven! Ragnarök
  2. puzzle_nodes

Source

puzzle_nodes / 15 / lib / stacker.rb

module Stacker
  class Interpreter
    attr_reader :stack

    COMMANDS = %w[ADD SUBTRACT]

    def initialize
      @stack = []
      @input_stack = []
    end

    def execute command
      unless commands.include? command
        command = if command == command.to_i.to_s
                    command.to_i
                  else
                    command.to_s
                  end

        @input_stack.push command
      else
        @stack << send(command)
        @input_stack.clear
      end
    end

    def commands
      public_methods.select{ |m| m =~ /[A-Z]+/ }.map(&:to_s)
    end

    def ADD
      @input_stack.inject(&:+)
    end

    def SUBTRACT
      @input_stack.inject(&:-)
    end

    def MULTIPLY
      @input_stack.inject(&:*)
    end

    def DIVIDE
      @input_stack.inject(&:/)
    end

    def MOD
      @input_stack.inject(:%)
    end
  end
end