Created
March 31, 2012 20:04
-
-
Save prodis/2267968 to your computer and use it in GitHub Desktop.
Ruby Fundamental - Hooks (ganchos) de inclusão e extensão de módulos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SomeClass1 | |
include SomeInstanceMethods | |
end | |
# => Module SomeInstanceMethods has been included by SomeClass1. | |
some_class = SomeClass1.new | |
some_class.some_instance_method # => Some instance method... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SomeClass2 | |
extend SomeClassMethods | |
end | |
# => Module SomeClassMethods has been extended by SomeClass2. | |
SomeClass2.some_class_method # => Some class method... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SomeClass3 | |
include SomeInstanceMethods | |
extend SomeClassMethods | |
end | |
# => Module SomeInstanceMethods has been included by SomeClass3. | |
# => Module SomeClassMethods has been extended by SomeClass3. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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