Xavier Noria wrote:
> On Jan 4, 2007, at 9:09 PM, Jeff wrote:
>
> > I happened to be reading dependencies.rb in the Rails source, and it
> > starts like this:
> >
> > require 'set'
> > require File.dirname(__FILE__) + '/core_ext/module/
> > attribute_accessors'
> > require File.dirname(__FILE__) + '/core_ext/load_error'
> > require File.dirname(__FILE__) + '/core_ext/kernel'
> >
> > module Dependencies #:nodoc:
> >   extend self
> > ...
> >
> >
> > What is the "extend self" doing?  I thought at the top a module,
> > 'self'
> > was pretty much an empty context at that point... but I guess not,
> > since the writer obviously thinks self contains something worth
> > extending...?
>
> It extends the very module object. That's one-liner to add all the
> module instance methods as module functions. That is
>
>    module Foo
>      extend self
>      def foo
>        'foo'
>      end
>    end
>
> allows the call Foo.foo.
>
> To do it by hand you'd add a
>
>    module_function :method
>
> for each method.

Not quite the same however. Using module_function actually creates a
new method that is a copy of the first. extend_self OTOH adds the
module to it's own metaclass' inheritance chain, so in that case they
are the same method.

T.