Class: Inputomatic::ArithmeticInterpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/inputomatic/arithmetic_interpreter.rb

Constant Summary collapse

CURRENCY_SYMBOLS =
"$£¢€¥₩₪₹₫₴₱₲₳₵₸₺₼₽៛₠₡₢₣₤₥₦₧₨₩₪₫€₭₮₯₰₱₲₳₴₵₶₷₸₹₺₻₼₽₾₿ℳ".chars.freeze

Instance Method Summary collapse

Constructor Details

#initialize(input, decimal_separator: '.', thousands_separator: ',') ⇒ ArithmeticInterpreter

Returns a new instance of ArithmeticInterpreter.



5
6
7
8
9
10
11
12
# File 'lib/inputomatic/arithmetic_interpreter.rb', line 5

def initialize(input, decimal_separator: '.', thousands_separator: ',')
  @input = input
    .gsub(/\s+/, '')
    .gsub(thousands_separator, '')
    .gsub(Regexp.union(CURRENCY_SYMBOLS), '')
  @decimal_separator = decimal_separator
  @index = 0
end

Instance Method Details

#parseObject



14
15
16
17
18
19
20
# File 'lib/inputomatic/arithmetic_interpreter.rb', line 14

def parse
  result = expression
  if @index < @input.length
    raise "Unexpected character: #{@input[@index]}"
  end
  result
end