Last active
August 29, 2015 13:58
-
-
Save willmcneilly/10008643 to your computer and use it in GitHub Desktop.
A Little Ruby Exercises, Chapter 1 https://dl.dropboxusercontent.com/u/40954128/a-little-ruby-a-lot-of-objects/Chapter1.pdf
This file contains 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
# Takes an interger and outputs as a string representation | |
def ascending?(first, second, third) | |
first < second && second < third | |
end | |
def descending?(first, second, third) | |
first > second && second > third | |
end | |
def never_descending?(first, second, third) | |
first <= second && second <= third | |
end | |
class FunnyNumber | |
def initialize(int_rep) | |
@rep = "n" * int_rep | |
end | |
def inspect | |
"FunnyNumber (#{@rep.length}) #{@rep}" | |
end | |
def as_integer | |
@rep.length | |
end | |
def <(other) | |
self.as_integer < other.as_integer | |
end | |
def >(other) | |
self.as_integer > other.as_integer | |
end | |
def <=(other) | |
self.as_integer <= other.as_integer | |
end | |
end | |
new_one = FunnyNumber.new(13) | |
new_two = FunnyNumber.new(33) | |
new_three = FunnyNumber.new(53) | |
new_four = FunnyNumber.new(1) | |
p new_one < new_two | |
p ascending?(new_one, new_two, new_three) | |
p descending?(new_three, new_two, new_one) | |
p never_descending?(new_one, new_two, new_four) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment