Created
January 22, 2011 20:58
-
-
Save jacegu/791469 to your computer and use it in GitHub Desktop.
A little example of mixing in Comparable module
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
class ComputerBrand | |
include Comparable | |
attr_reader :name | |
def initialize(name) | |
@name = name | |
end | |
def <=>(other) | |
return -1 if @name > other.name | |
return 0 if @name == other.name | |
return 1 if @name < other.name | |
end | |
end | |
apple = ComputerBrand.new("apple") | |
dell = ComputerBrand.new("dell") | |
hp = ComputerBrand.new("hp") | |
microsoft = ComputerBrand.new("microsoft") | |
puts "Apple is better than Dell: #{apple > dell}" | |
puts "Apple is better than HP: #{apple > hp}" | |
puts "Apple is better than Microsoft: #{apple > microsoft}" | |
puts "Dell is between Apple and HP: #{dell.between?(hp, apple)}" | |
puts "Microsoft is worse than HP: #{microsoft <= hp}" | |
puts "Microsoft is better than Microsoft: #{microsoft > microsoft}" | |
puts "Microsoft is worse than Microsoft: #{microsoft < microsoft}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment