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 = []
    end

    def execute command
      unless commands.include? command
        @stack.push command
      else
        @stack = send command
      end
    end

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

    def ADD
      [@stack.map(&:to_i).inject(&:+)]
    end

    def SUBTRACT
      [@stack.map(&:to_i).inject(&:-)]
    end

    def MULTIPLY
      [@stack.map(&:to_i).inject(&:*)]
    end

    def DIVIDE
      [@stack.map(&:to_i).inject(&:/)]
    end
  end
end