Last active
April 17, 2022 21:57
-
-
Save thisismydesign/ff906cc56e22ccd5cc7253de41402611 to your computer and use it in GitHub Desktop.
Ruby: how to use self.included meaningfully
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
module CreateClassMethodsUponInclude | |
def self.included(base) | |
# does what we want | |
end | |
end | |
module MyModule | |
include CreateClassMethodsUponInclude | |
# ... | |
end |
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
module CreateClassMethodsUponInclude | |
def self.included(base) | |
base.define_singleton_method :included do |base| | |
base.extend(self) | |
end | |
end | |
end | |
module MyModule | |
include CreateClassMethodsUponInclude | |
def something | |
'something ' | |
end | |
end | |
class MyClass | |
include MyModule | |
end | |
p MyClass.something + MyClass.new.something + 'awesomeness' |
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
def self.included(base) | |
base.extend(self) | |
end |
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
def self.included(base) | |
def base.class_method | |
# ... | |
end | |
end |
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
class MyClass | |
include SomeLogic | |
extend SomeLogic | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment