122

In Ruby, I understand the basic idea of extend. However, what's happening in this segment of code? Specifically, what does extend do? Is it just a convenient way of making the instance methods into class methods? Why would you do it this way rather than specifying class methods from the beginning?

module Rake
  include Test::Unit::Assertions

  def run_tests # etc.
  end

  # what does the next line do?
  extend self
end

6 Answers 6

121

It is a convenient way to make instance methods into class methods. But you can also use it as a more efficient singleton.

4
  • 2
    Why is that kind of singleton more efficient?
    – xuuso
    Jun 16, 2016 at 10:53
  • 7
    Your link has rotten my friend.
    – Ulysse BN
    Dec 20, 2018 at 8:51
  • 2
    Updated this answer with a link to archive.org May 29, 2019 at 11:32
  • 5
    This answer is inadequate because it doesn't explain how the keyword in question makes instance methods into class methods. It also doesn't explain what a 'more efficient singleton' is, or what extend self has to do with that.
    – jayqui
    Jul 26, 2019 at 17:51
30

In a module, self is the module class itself. So for example

puts self

will return Rake so,

extend self

basically makes the instance methods defined in Rake available to it, so you can do

Rake.run_tests
26

For me it always helps to think of extend as include inside the singleton class (also known as meta or eigen class).

You probably know that methods defined inside the singleton class are basically class methods:

module A
  class << self
    def x
      puts 'x'
    end
  end
end

A.x #=> 'x'

Now that we know that, extend will include the methods in the module inside the singleton class and thus expose them as class methods:

module A
  class << self
    include A

    def x
      puts 'x'
    end
  end

  def y
    puts 'y'
  end
end

A.x #=> 'x'
A.y #=> 'y'
17

To avoid link rot, the blog post of Chris Wanstrath linked by user83510 is reposted below (with his permission). Still, nothing beats an original, so use his link for as long as it continues to work.


→ singin' singletons 18 November 2008 There’s stuff I just don’t understand. David Bowie, for instance. Or the Southern Hemisphere. But nothing quite boggles my mind like Ruby’s Singleton. Because really, it’s totally unnecessary.

Here’s what they want you to do with your code:

require 'net/http'

# first you setup your singleton
class Cheat
  include Singleton

  def initialize
    @host = 'http://cheat.errtheblog.com/'
    @http = Net::HTTP.start(URI.parse(@host).host)
  end


  def sheet(name)
    @http.get("/s/#{name}").body
  end
end

# then you use it
Cheat.instance.sheet 'migrations'
Cheat.instance.sheet 'yahoo_ceo'

But that’s crazy. Fight the power.

require 'net/http'

# here's how we roll
module Cheat
  extend self

  def host
    @host ||= 'http://cheat.errtheblog.com/'
  end

  def http
    @http ||= Net::HTTP.start(URI.parse(host).host)
  end

  def sheet(name)
    http.get("/s/#{name}").body
  end
end

# then you use it
Cheat.sheet 'migrations'
Cheat.sheet 'singletons'

Any why not? The API is more concise, the code is easier to test, mock, and stub, and it’s still dead simple to convert into a proper class should the need arise.

(( copyright ought ten chris wanstrath ))

1
  • Another way to avoid linkrot is to use something like the wayback machine -- web.archive.org -- it keeps a history of pages across the web, I've found it quite helpful in many cases of linkrot anyway.
    – Kem Mason
    May 13, 2013 at 22:52
4

extend self includes all the existing instance methods as module methods. This is equivalent to saying extend Rake. Also Rake is an object of class Module.

Another way to achieve equivalent behavior will be:

module Rake
  include Test::Unit::Assertions

  def run_tests # etc.
  end

end 

Rake.extend(Rake)

This can be used to define self contained modules with private methods.

0

https://benkoshy.github.io/2024/01/25/what-does-extend-self-mean-in-ruby.html

A faithful reproduction of the above is listed below:

This is best answered with a code example - which will hopefully make manifest the answer:


module Car
    extend self

    def drive
        puts "Don't stop me (driving)! I'm having such a good time, havin' a ball!"
    end

end

class Toyota
    include Car
end

t = Toyota.new
t.drive
# => Don't stop me (driving)! I'm having such a good time, havin' a ball!


Car.drive 
# => Don't stop me (driving)! I'm having such a good time, havin' a ball!
# extend self makes the above possible.

# Let's go one step further:
# Toyota.drive # will this work? What do you think?

class Toyota
    extend Car
end

Toyota.drive # now will it work?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.