Created
November 30, 2020 17:25
-
-
Save xtrator/89125032bd6adbc2bee5dbf216ca9b0a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# rubocop:disable all | |
require "ruby_badge_numbers/version" | |
module RubyBadgeNumbers | |
# Your code goes here... | |
class PositiveNumber | |
attr_reader :pred | |
def initialize(zero, pred) | |
@zero = zero | |
@pred = pred | |
end | |
def succ | |
@succ ||= PositiveNumber.new(@zero, self) | |
end | |
def +(number) | |
current = @zero | |
sum = self | |
until current == number | |
current = current.succ | |
sum = sum.succ | |
end | |
sum | |
end | |
def -(number) | |
# does not take into account negative numbers | |
current = number | |
sum = self | |
until current == @zero | |
current = current.pred | |
sum = sum.pred | |
end | |
sum | |
end | |
end | |
class Zero | |
def succ | |
# PostiveNumber.new(zero, pred) | |
@succ ||= PositiveNumber.new(self, self) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment