James Gray
1/4/2007 8:17:00 PM
On Jan 4, 2007, at 2: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...?
Well, like any method call in Ruby, there is a receiver here. In
this case, it's just the implicit self, so the call is actually
self.extend(self). Self, in that context, is the module
Dependancies. Dependancies.extend(Dependancies) means, duplicate all
the instance methods as module methods.
Does that make sense?
James Edward Gray II