Created
April 2, 2015 09:03
-
-
Save jcinnamond/bcbf4ab7c07ff34c8d50 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
class Zero | |
def pred | |
self | |
end | |
def succ | |
Number.new(self) | |
end | |
def +(other) | |
other | |
end | |
def to_s | |
"" | |
end | |
def inspect | |
"Zero" | |
end | |
end | |
class Number | |
attr_reader :pred | |
def initialize(pred = NotANumber.new) | |
@pred = pred | |
end | |
def succ | |
Number.new(self) | |
end | |
def +(other) | |
Number.new(pred + Number.new(other.pred)) | |
end | |
def to_s | |
"." + @pred.to_s | |
end | |
def inspect | |
"<Number #{to_s} @pred=#{@pred.inspect}>" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Weird unnecessary complexity. There is no need to use
Number.new(other.pred)
in the addition as that is identical toother
. The definition of plus should simply be: