モジュールとmixin

Rubyのモジュールをincludeすると、実質的に多重継承ができます。

module DebugPrint
    def debug_print
        p self
    end
end

class Rubyco
    include DebugPrint

    def initialize(name)
        @name = name
    end
end

class Diamond
    include DebugPrint

    def initialize(x, y, z)
        @x, @y, @z = x, y, z
    end
end

class Gargoyle
    include DebugPrint

    def initialize(pro, con)
        @pro, @con = pro, con
    end

    def debug_print
        print "Gargoyle: pro = #{@pro}, con = #{@con}\n"
    end
end

arr = [
    Rubyco.new("rubyco"),
    Diamond.new(3, 1, 4),
    Gargoyle.new("Pride", "Prejudice")
]

arr.each { |e|
    e.debug_print
}
__END__
=>
#<Rubyco:0x2945f68 @name="rubyco">
#<Diamond:0x2945f50 @y=1, @x=3, @z=4>
Gargoyle: pro = Pride, con = Prejudice