Ruby Quicktips Logo

Ruby Quicktips

Random Ruby and Rails tips.
This blog is dedicated to deliver short, interesting and practical tidbits of the Ruby language and Ruby on Rails framework. Read more...

Your submissions are more than welcome!
Sep 17 ’10

Include vs. Extend

You can either use include or extend to mix in a module’s functionality into a class. The difference is this:

  • include makes the module’s methods available to the instance of a class, while
  • extend makes these methods available to the class itself.

Check out this example:

module Greetings
  def say_hello
    puts "Hello!"

class Human
  include Greetings
end # => "Hello!"
Human.say_hello     # NoMethodError

class Robot
  extend Greetings
end # NoMethodError
Robot.say_hello     # => "Hello!"

If you want more information on include vs. extend, I recommend the following resources:

20 notes 0 comments

  1. blazeeboy reblogged this from rubyquicktips
  2. geekontheway reblogged this from rubyquicktips
  3. suryar-tripathi reblogged this from rubyquicktips
  4. atm09td reblogged this from rubyquicktips
  5. rubyquicktips posted this


You can use HTML tags for formatting. Wrap code in <code> tags and multiple lines of code in <pre><code> tags.

blog comments powered by Disqus