Sha256: a2c0c1e36224877d96ecbb1e533783078ea3a679ace928c1ebd904ef07ddf65c

Contents?: true

Size: 697 Bytes

Versions: 1

Compression:

Stored size: 697 Bytes

Contents

module ReversePolishCalculator
  class Input
    
    attr_reader :value
    
    def initialize(value)
      @value = value
      exit if exited?
    end
    
    def exited?
      value.downcase == 'q'
    end
    
    def invoke(input_1, input_2 = nil)
      if math_method?
        Output.add "calculated: #{value}(#{input_1.to_f})"
        Math.send(value, input_1.to_f)
      else
        Output.add "calculated: #{input_1.to_f} #{value} #{input_2.to_f}"
        input_1.to_f.send(value, input_2.to_f)
      end
    end
    
    def math_method?
      Math.respond_to?(value)
    end
    
    def operand?
      !!value[/\d/]
    end
    
    def to_f
      value.to_f
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reverse-polish-calculator-0.0.1 lib/reverse-polish-calculator/input.rb