Created
November 30, 2020 16:55
-
-
Save xtrator/be87f44132a0d942864dcca924716e85 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
require "ruby_badge_numbers/version" | |
module RubyBadgeNumbers | |
# Your code goes here... | |
class PositiveNumber | |
attr_reader :zero, :pos | |
def initialize(zero, pos) | |
@pos = pos # su posicion en la cadena | |
@zero = zero # de esta forma todos las intancias tienen referencia al mismo objeto zero | |
@succ = nil | |
end | |
def succ | |
@succ ||= PositiveNumber.new(@zero, @pos + 1) | |
end | |
def +(number) | |
sum = @zero | |
(self.pos + number.pos).times do | |
sum = sum.succ | |
end | |
sum | |
end | |
def -(number) | |
subs = @zero | |
(self.pos - number.pos).times do | |
subs = subs.succ | |
end | |
subs | |
end | |
end | |
class Zero | |
attr_reader :pos | |
def initialize | |
@pos = 0 | |
@succ = nil | |
end | |
def succ | |
@succ ||= PositiveNumber.new(self, @pos + 1) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment