public
Created

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
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 #"

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.