1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.
Michael Morin

A Quick Peek at Ruby: 'Include' vs. 'Require'

By October 23, 2008

Follow me on:

After our recent post about the require method, we received the following question from one of our readers. After dropping him an email to answer it, it occurred to us that he might not be only one wondering, so we're posting the question and answer here as a Quick Peek at Ruby.

What's the difference between "include" and "require" in Ruby?

Answer:

The include and require methods do very different things.

The require method does what include does in most other programming languages: run another file. It also tracks what you've required in the past and won't require the same file twice. To run another file without this added functionality, you can use the load method.

The include method takes all the methods from another module and includes them into the current module. This is a language-level thing as opposed to a file-level thing as with require. The include method is the primary way to "extend" classes with other modules (usually referred to as mix-ins). For example, if your class defines the method "each", you can include the mixin module Enumerable and it can act as a collection. This can be confusing as the include verb is used very differently in other languages.

Comments
November 6, 2008 at 10:10 am
(1) Ujjwal Trivedi says:

yay! and I am that reader! :-)

September 25, 2012 at 8:55 pm
(2) Erin Parker says:

Thank you, this was very helpful!

October 10, 2012 at 9:06 pm
(3) Rebekah says:

Yes, so am I . I’ve been back to this article three times now!

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>
Top Related Searches
  • peek
  • ruby
    1. About.com
    2. Technology
    3. Ruby

    ©2014 About.com. All rights reserved.