Skip to content

Instantly share code, notes, and snippets.

@laser
Last active January 4, 2016 00:39
Show Gist options
  • Save laser/8543065 to your computer and use it in GitHub Desktop.
Save laser/8543065 to your computer and use it in GitHub Desktop.
Functional OO Calculator
class StatefulCalculator
def initialize(total)
@total = total
end
def add(x)
StatefulCalculator.new(@total + x)
end
def sub(x)
StatefulCalculator.new(@total - x)
end
def mul(x)
StatefulCalculator.new(@total * x)
end
def div(denom)
StatefulCalculator.new(@total / x)
end
def result
@total
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment