Created
March 25, 2018 09:13
-
-
Save barsbek/4dd571117a3a2cb9069cffc38bb3ca64 to your computer and use it in GitHub Desktop.
ruby: include class and instance methods from single module
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 SomeModule | |
# instance methods: | |
def some_instance_method | |
# do something here | |
end | |
# module, which contains class methods | |
module ClassMethods | |
# class methods: | |
def some_class_method | |
# do something | |
end | |
end | |
# included method is invoked every time module is included | |
# in this case included is invoked, when SomeModule is included | |
def self.included(base) | |
base.extend(ClassMethods) | |
end | |
end | |
class SomeClass | |
include SomeModule | |
# now, methods from SomeModule are available in SomeClass and its instances | |
end | |
SomeClass.new.some_instance_method | |
SomeClass.some_class_method |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment