module Mod
def x
puts “x”
end
end
class C1
extend Mod
end
class C2
include Mod
end
C1.x – will work
C2.x – will not, but C2.new.x will
In essence:
- extend – class level
- include – instance level
None of the books explain this properly. They go into great detail with lots of pictures, but this simple thing is not explained anywhere, or is wrapped up in some stupid example that is really trying to make some other point. In the end I looked at the supplied code and worked out how to do it. Cost me a couple of hours and really very trivial.
You can also have private methods in your modules and they all go in nice ’n easy.