Comparable module can provide the other basic comparison operators and between?.
class Song
include Comparable
attr_accessor :length
def <=>(other)
@length <=> other.length
end
def initialize(song_name, length)
@song_name = song_name
@length = length
end
end
a = Song.new('Rock around the clock', 143)
b = Song.new('Bohemian Rhapsody', 544)
c = Song.new('Minute Waltz', 60)
# Here are the results of including the Comparable module:
a < b
b >= c
c > a
a.between?(b,c)
Related examples in the same category