rubyの仕様書
https://www.ruby-lang.org/en/documentation/faq/8/
Give me an example of a mixin
The module Comparable provides a variety of comparison operators (<, <=, ==, >=, >, between?). It defines these in terms of calls to the general comparison method, <=>. However, it does not itself define <=>.
Say you want to create a class where comparisons are based on the number of legs an animal has:
class Animal
include Comparable
attr_reader :legs
def initialize(name, legs)
@name, @legs = name, legs
end
def <=>(other)
legs <=> other.legs
end
def inspect
@name
end
end
c = Animal.new("cat", 4)
s = Animal.new("snake", 0)
p = Animal.new("parrot", 2)
c < s # => false
s < c # => true
p >= s # => true
p.between?(s, c) # => true
[p, s, c].sort # => [snake, parrot, cat]