Created
August 21, 2018 12:46
-
-
Save thisismydesign/a07b034494e33ea0dc125eb30733cf2d to your computer and use it in GitHub Desktop.
Ruby: how to use self.included meaningfully, solution and use case
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' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment