Created
April 6, 2014 17:25
-
-
Save willmcneilly/10009005 to your computer and use it in GitHub Desktop.
A Little Ruby Exercises, Chapter 2a - https://dl.dropboxusercontent.com/u/40954128/a-little-ruby-a-lot-of-objects/Chapter2.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 | |
# We include the Comparable protocol which implements comparison operators for a | |
# class as long as <=> is defined in it. | |
class FunnyNumber | |
include Comparable | |
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 | |
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