Created
September 9, 2023 17:20
-
-
Save maxim/ee976d14c10c72b6991ba196e87940d0 to your computer and use it in GitHub Desktop.
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
class Severity | |
include Comparable | |
attr_reader :value | |
ORDER = %i[low medium high] | |
def self.[](value) | |
new(value) | |
end | |
def initialize(value) | |
@value = value | |
end | |
def <=>(other) | |
ORDER.index(@value) <=> ORDER.index(other.value) | |
end | |
def inspect | |
"Severity[:#{@value}]" | |
end | |
end | |
pp [Severity[:high], Severity[:low]].sort | |
# Output | |
# [Severity[:low], Severity[:high]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment