Created
September 4, 2016 13:22
-
-
Save esquinas/343f57919c2e4f5643ac8a123cc5c0e3 to your computer and use it in GitHub Desktop.
Countdown ruby class is for making countdown ranges without #reverse
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
# 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