Created
November 13, 2020 00:23
-
-
Save mattantonelli/f00556bacd8c335068bfe0f1903a6ff2 to your computer and use it in GitHub Desktop.
A totally acceptable way of producing a reverse range in Ruby.
This file contains hidden or 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
| #!/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 } |
Author
mattantonelli
commented
Nov 13, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment