Skip to content

Instantly share code, notes, and snippets.

@mattantonelli
Created November 13, 2020 00:23
Show Gist options
  • Select an option

  • Save mattantonelli/f00556bacd8c335068bfe0f1903a6ff2 to your computer and use it in GitHub Desktop.

Select an option

Save mattantonelli/f00556bacd8c335068bfe0f1903a6ff2 to your computer and use it in GitHub Desktop.
A totally acceptable way of producing a reverse range in Ruby.
#!/usr/bin/env ruby
class CoolNum
attr :x
def initialize(x)
@x = x
end
def succ
CoolNum.new(@x - 1)
end
def <=>(other)
-@x <=> other.x
end
def to_s
sprintf "%d", @x
end
end
(CoolNum.new(10)..CoolNum.new(0)).each { |i| puts i }
@mattantonelli
Copy link
Copy Markdown
Author

10
9
8
7
6
5
4
3
2
1
0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment