What does "extend self" do?

Discussion in 'Ruby' started by Jeff, Jan 4, 2007.

  1. Jeff

    Jeff Guest

    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

    Jeff, Jan 4, 2007
    1. Advertisements

  2. Alle 21:09, gioved=EC 4 gennaio 2007, Jeff ha scritto:
    In a module definition, outside methods, self refers to the module itself, =
    it happens inside the definition of a class. For instance, the following co=

    module MyModule
    puts self.class
    puts self.name


    Stefano Crocco, Jan 4, 2007
    1. Advertisements

  3. Jeff

    dblack Guest

    Hi --

    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

    will print:


    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"
    extend self

    M.greet # => hi

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


    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)
    dblack, Jan 4, 2007
  4. 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
    James Edward Gray II, Jan 4, 2007
  5. Jeff

    Xavier Noria Guest

    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

    allows the call Foo.foo.

    To do it by hand you'd add a

    module_function :method

    for each method.

    -- fxn
    Xavier Noria, Jan 4, 2007
  6. Jeff

    Trans Guest

    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.

    Trans, Jan 4, 2007
  7. Jeff

    Xavier Noria Guest

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

    Thank you!

    -- fxn
    Xavier Noria, Jan 4, 2007
  8. Jeff

    Jeff Guest

    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, Jan 4, 2007
  9. Jeff

    Jeff Guest

    (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, Jan 5, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.