Skip to content

Instantly share code, notes, and snippets.

@thisismydesign
Last active April 17, 2022 21:57
Show Gist options
  • Save thisismydesign/ff906cc56e22ccd5cc7253de41402611 to your computer and use it in GitHub Desktop.
Save thisismydesign/ff906cc56e22ccd5cc7253de41402611 to your computer and use it in GitHub Desktop.
Ruby: how to use self.included meaningfully
module CreateClassMethodsUponInclude
def self.included(base)
# does what we want
end
end
module MyModule
include CreateClassMethodsUponInclude
# ...
end
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'
def self.included(base)
base.extend(self)
end
def self.included(base)
def base.class_method
# ...
end
end
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