Skip to content
golive edited this page Mar 7, 2011 · 1 revision

Creació de Mixin amb mètodes de classe i d'instància.

module M
  def self.included(base)
    base.extend ClassMethods
  end

  def instance_method
    puts "instance method from M"
  end
  
  module ClassMethods
    def class_method
      puts "class method from M"
    end
  end
end

class C1
  include M
end

class C2 < C1
  def instance_method
    puts "instance method from C2"
    super
  end
  
  def self.class_method
    puts "class method from C2"
    super
  end
end

C2.class_method         # => "class method from C2\nclass method from M"
C2.new.instance_method  # => "instance method from C2\ninstance method from M"
Clone this wiki locally