Created
August 21, 2014 02:07
-
-
Save wrpinheiro/e2571ebfbfc1f8e08f24 to your computer and use it in GitHub Desktop.
Ruby include & extend
This file contains 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
# Version 1 | |
# Some basic include & extend approach | |
module AnyModule | |
def an_instance_method | |
end | |
module ClassMethods | |
def a_class_method | |
end | |
end | |
end | |
class AnyClass | |
include AnyModule | |
extend AnyModule::ClassMethods | |
end | |
# Version 2 | |
# Use method hook self.included | |
module AnyModule | |
def self.included(base) | |
base.extend(ClassMethods) | |
end | |
def an_instance_method | |
end | |
module ClassMethods | |
def a_class_method | |
end | |
end | |
end | |
class AnyClass | |
include AnyModule | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment