From: Gavin Sinclair Date: 2003-01-23T02:59:31+09:00 Subject: Re: extend (Re: a single class that supports multiple facets/interfaces) On Thursday, January 23, 2003, 4:12:58 AM, Gavin wrote: >> Well, here's some cool code then: > ... and some more: ... and finally (I just noticed this in fileutils.rb) Ever find it annoying when you create a module which is basically intended to be a namespace/container for a set of functions, and they don't work because you have to use "public_module_method" or whatever? Here's another way. module M def hi "hi" end end M.extend M # These module M # three are extend M end module M # equivalent extend self end M.hi # => "hi" The code I saw opted for "extend self" after all the methods had been defined. This had me scratching my head wondering what it meant. A comment up the top switched the light on. But it could have been written "extend FileUtils", which would have been a bit clearer, and it could have been done up the top, with a comment saying all methods are hearby public module methods. That's right, it could be up the top. Check this: >> module M >> def hi >> "hi" >> end >> extend M >> def lo >> "lo" >> end >> end => nil >> M.hi => "hi" >> M.lo => "lo" Apparently, using "extend" merely hooks something up behind the scenes, rather than dumping a lot of information into the target module. This is not particularly surprising, but it's still nice to have a language that lets you play around like this and experiment. Gavin