Skip to content

Instantly share code, notes, and snippets.

@ruprict
Created April 23, 2012 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruprict/2470592 to your computer and use it in GitHub Desktop.
Save ruprict/2470592 to your computer and use it in GitHub Desktop.
module MyModule
def self.included(base)
base.extend(ClassMethods)
end
def hellofrominstance
puts "hello from #{self}"
end
# Conventionally named ClassMethods
module ClassMethods
def hellofromclass
puts "Hello from #{self}"
end
end
end
# including MyModule will call the hook (#included) which
# extends the class methods
class Blah
include MyModule
end
pry(main)> Blah.hellofromclass
#=> "Hello from Blah"
pry(main)> Blah.new.hellofrominstance
#=> "hello from #"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment