Ruby中实现module继承

时间:2021-10-25 21:22:07

module FooModule
  def self.included base
    base.extend ClassMethods
  end

module ClassMethods
    def bar
      puts "module"
    end
  end
end

module Klass
  include FooModule

def self.bar
    puts 'class'
    super
  end
end

Klass.bar #=> class

原文: http://tech.tulentsev.com/2012/02/ruby-how-to-override-class-method-with-a-module/