Hi --

On Fri, 5 Jan 2007, 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...?

self is never empty; it's always something.  At the top of a module
definition, it's the module object itself:

   module M
     p self
   end

will print:

   M

So what extend self does is it extends the module object by making the
instance methods in the module available to it:

   module M
     def greet
       puts "hi"
     end
     extend self
   end

   M.greet  # =>  hi

Now the object M has access to the instance methods defined in M --
which it also happens to *be* :-)


David

-- 
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
    (See what readers are saying!  http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)