Skip to content

Instantly share code, notes, and snippets.

@willmcneilly
Created April 6, 2014 17:25
Show Gist options
  • Save willmcneilly/10009005 to your computer and use it in GitHub Desktop.
Save willmcneilly/10009005 to your computer and use it in GitHub Desktop.
# 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