Created
June 25, 2019 15:55
-
-
Save matthewrudy/431198f42da890e33672700bee2eaaef to your computer and use it in GitHub Desktop.
Example of using Protected methods for comparisons
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 CompetitiveEater | |
include Comparable | |
def <=>(other) | |
spicy_tolerance <=> other.spicy_tolerance | |
end | |
def initialize(spicy_tolerance) | |
@spicy_tolerance = spicy_tolerance | |
end | |
protected | |
# a secret, other non-eaters won't understand | |
# but we know this is the most important metric | |
attr_reader :spicy_tolerance | |
end | |
one = CompetitiveEater.new(1) | |
million = CompetitiveEater.new(1_000_000) | |
puts "is one better than one million?: #{one > million}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment