class SimpleMath::Calculator

Calculator

Attributes

start[RW]

Public Class Methods

new(start:) click to toggle source
# File lib/simple_math/calculator.rb, line 14
def initialize(start:)
  @start = start
end
run(start: 0, &block) click to toggle source
# File lib/simple_math/calculator.rb, line 8
def self.run(start: 0, &block)
  new(start: start).send(:calculate, &block)
end

Private Instance Methods

calculate() { |operations_hash| ... } click to toggle source
# File lib/simple_math/calculator.rb, line 18
def calculate
  operations_hash = operations(start)
  yield operations_hash if block_given?
  operations_hash[:total].call
end
operations(total) click to toggle source
# File lib/simple_math/calculator.rb, line 24
def operations(total)
  {
    total: -> { total },
    '+': ->(number) { total += number.to_f }.curry,
    '-': ->(number) { total -= number.to_f }.curry,
    '*': ->(number) { total *= number.to_f }.curry,
    '/': ->(number) { total /= number.to_f }.curry
  }
end