Forum: Ruby What does "extend self" do?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8217faf2bfdfa7daf10135d41ddd421e?d=identicon&s=25 Jeff Cohen (jeff)
on 2007-01-06 19:30
(Received via mailing list)
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...?

Jeff
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (Guest)
on 2007-01-06 19:30
(Received via mailing list)
On Jan 4, 2007, at 9:09 PM, Jeff wrote:

>   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.

-- fxn
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2007-01-06 19:30
(Received via mailing list)
Alle 21:09, giovedì 4 gennaio 2007, Jeff ha scritto:
> ...
>
>
> 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...?
>
> Jeff

In a module definition, outside methods, self refers to the module
itself, as
it happens inside the definition of a class. For instance, the following
code

module MyModule
  puts self.class
  puts self.name
end

gives

Module
MyModule
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-01-06 19:30
(Received via mailing list)
Hi --

On Fri, 5 Jan 2007, Jeff wrote:

> ...
>
>
> 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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-01-06 19:31
(Received via mailing list)
On Jan 4, 2007, at 2:09 PM, Jeff wrote:

>   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
8217faf2bfdfa7daf10135d41ddd421e?d=identicon&s=25 Jeff Cohen (jeff)
on 2007-01-19 16:29
(Received via mailing list)
Google seems to have lost my previous 2 attempts to reply to this
thread, here we go again...

Awesome, thanks for the help everyone!  For some reason when I saw the
"extend" at the top of the class, instead of at the bottom, I didn't
see how it know about the instance methods that follow.  Doh!

Thanks again,
Jeff
45196398e9685000d195ec626d477f0e?d=identicon&s=25 Trans (Guest)
on 2007-01-19 16:30
(Received via mailing list)
Xavier Noria wrote:
> >
>
> 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.
8217faf2bfdfa7daf10135d41ddd421e?d=identicon&s=25 Jeff Cohen (jeff)
on 2007-01-19 16:30
(Received via mailing list)
dblack@wobblini.net wrote:
> So what extend self does is it extends the module object by making the
> instance methods in the module available to it:

(and to all the others who said similar things)

Thanks everyone!  For some reason I thought that order was important,
and having it appear before defining the instance methods would be
futile.  As soon as I started reading all the replies I realized I was
being silly.

Thanks again.
Jeff
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (Guest)
on 2007-01-19 16:31
(Received via mailing list)
On Jan 4, 2007, at 10:05 PM, Trans wrote:

>> 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.

Right, I just meant them to be conceptually similar to add some
redundancy to the explanation. But it wasn't exact.

Thank you!

-- fxn
This topic is locked and can not be replied to.