Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby Fundamental - Hooks (ganchos) de inclusão e extensão de módulos
module SomeInstanceMethods
def self.included(base)
puts "Module #{self} has been included by #{base}."
end
def some_instance_method
puts "Some instance method..."
end
end
class SomeClass1
include SomeInstanceMethods
end
# => Module SomeInstanceMethods has been included by SomeClass1.
some_class = SomeClass1.new
some_class.some_instance_method # => Some instance method...
module SomeClassMethods
def self.extended(base)
puts "Module #{self} has been extended by #{base}."
end
def some_class_method
puts "Some class method..."
end
end
class SomeClass2
extend SomeClassMethods
end
# => Module SomeClassMethods has been extended by SomeClass2.
SomeClass2.some_class_method # => Some class method...
class SomeClass3
include SomeInstanceMethods
extend SomeClassMethods
end
# => Module SomeInstanceMethods has been included by SomeClass3.
# => Module SomeClassMethods has been extended by SomeClass3.
module SomeIncludedModule
def self.included(base)
base.send(:include, InstanceMethods)
base.extend(ClassMethods)
puts "Module #{self} has been included by #{base}."
end
module InstanceMethods
def some_instance_method
puts "Some instance method..."
end
end
module ClassMethods
def some_class_method
puts "Some class method..."
end
end
end
class SomeClass4
include SomeIncludedModule
end
# => Module SomeIncludedModule has been included by SomeClass4.
SomeClass4.new.some_instance_method # => Some instance method...
SomeClass4.some_class_method # => Some class method...
module SomeExtendedModule
def self.extended(base)
base.send(:include, InstanceMethods)
base.extend(ClassMethods)
puts "Module #{self} has been extended by #{base}."
end
module InstanceMethods
def some_instance_method
puts "Some instance method..."
end
end
module ClassMethods
def some_class_method
puts "Some class method..."
end
end
end
class SomeClass5
extend SomeExtendedModule
end
# => Module SomeExtendedModule has been extended by SomeClass5.
SomeClass5.new.some_instance_method # => Some instance method...
SomeClass5.some_class_method # => Some class method...
module SomeCoolModule
def self.included(base)
mix_modules!(base)
end
def self.extended(base)
mix_modules!(base)
end
private
def self.mix_modules!(base)
base.send(:include, InstanceMethods)
base.extend(ClassMethods)
end
module InstanceMethods
def some_instance_method
puts "Some instance method..."
end
end
module ClassMethods
def some_class_method
puts "Some class method..."
end
end
end
class SomeClass6
include SomeCoolModule
end
class SomeClass7
extend SomeCoolModule
end
SomeClass6.new.some_instance_method # => Some instance method...
SomeClass6.some_class_method # => Some class method...
SomeClass7.new.some_instance_method # => Some instance method...
SomeClass7.some_class_method # => Some class method...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment