Skip to content

Instantly share code, notes, and snippets.

@jacegu
Created January 22, 2011 20:58
Show Gist options
  • Save jacegu/791469 to your computer and use it in GitHub Desktop.
Save jacegu/791469 to your computer and use it in GitHub Desktop.
A little example of mixing in Comparable module
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