Skip to content

Instantly share code, notes, and snippets.

@esquinas
Created September 4, 2016 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esquinas/343f57919c2e4f5643ac8a123cc5c0e3 to your computer and use it in GitHub Desktop.
Save esquinas/343f57919c2e4f5643ac8a123cc5c0e3 to your computer and use it in GitHub Desktop.
Countdown ruby class is for making countdown ranges without #reverse
# Countdown class is for making countdown ranges without reversing.
# a = Countdown.new 5
# b = Countdown.new 1
# print (a..b).map(&:value).join(', ') # =>
# 5, 4, 3, 2, 1 => nil
class Countdown
VERSION = 0
attr_accessor :value
def initialize(value)
@value = value
end
def <=>(other)
# TODO: Test `-1 * (value <=> other.value)`
if value > other.value
-1
elsif value == other.value
0
else
1
end
end
def succ
self.class.new(value - 1)
end
alias next succ
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment