Skip to content

Instantly share code, notes, and snippets.

@barsbek
Created March 25, 2018 09:13
Show Gist options
  • Save barsbek/4dd571117a3a2cb9069cffc38bb3ca64 to your computer and use it in GitHub Desktop.
Save barsbek/4dd571117a3a2cb9069cffc38bb3ca64 to your computer and use it in GitHub Desktop.
ruby: include class and instance methods from single module
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