What is the difference between include and require in Ruby?
include
in Ruby
include
is a keyword used within a class or module to mix in methods (and constants) from another module as instance methods. Essentially, include
takes all the methods defined in a module and makes them available to instances of the class that includes it. Here’s a quick example:
module Greeter def greet "Hello!" end end class Person include Greeter end p = Person.new puts p.greet # => "Hello!"
- Mixing In: By writing
include Greeter
, thegreet
method becomes an instance method ofPerson
. - Inheritance-Like Mechanism: Ruby uses a chain called the “method lookup path.” When you include a module, Ruby inserts the module into this path between the class and its superclass, giving you a form of multiple inheritance.
- Include vs. Extend: In Ruby,
extend
is similar, but it adds the module’s methods as class methods instead of instance methods. For example,extend Greeter
inside a class would makegreet
a class method.
require
in Ruby
require
is used to load external files or libraries. It instructs Ruby to find a file (by checking the “load path”) and read or execute its contents. This is crucial for organizing your code into separate files and leveraging Ruby’s built-in or external libraries.
# load library from Ruby's standard library or a gem require 'json' # load a local file from your load path require 'my_local_library'
- Finding the File: By default, Ruby looks in the directories specified in
$LOAD_PATH
(orrbconfig
for built-in libraries). If it can’t find the file, it raises aLoadError
. - Require Once: Ruby will only load the file once. Subsequent
require "filename"
calls for the same file do nothing by default—this prevents repeated loading and potential redefinition errors. - Contrast with
load
:load 'filename.rb'
reads and parses the file every time you call it, regardless of whether it was previously loaded.
Key Differences
-
Purpose
include
: Mix a module into a class or another module, effectively copying its methods as instance methods.require
: Load an external file (Ruby file or library), making its definitions available in the current scope.
-
Usage Context
include
: Typically used inside a class or module definition.require
: Usually placed at the top of a Ruby file (outside any class or module) or inside a conditional if you conditionally load a library.
-
Multi-File vs. Single-File
include
: Only works on modules that are already loaded in your current runtime (either defined in the same file or previously required).require
: Retrieves and parses a file that may not yet be loaded, making new modules, classes, or methods accessible.
-
Effect on Method Lookup
include
: Alters the method lookup path by inserting the module. If you call an instance method, Ruby will look in your class, then in the included module, then up the inheritance hierarchy.require
: Doesn’t directly affect method lookup paths; instead, it provides new classes/modules/definitions for you to reference afterward.
-
Common Pitfalls
- Using
include
for External Files: Some beginners confuseinclude
as a way to “pull in” external code. This won’t work unless that external code is defined as a module in the current runtime. If you want to load code from another file, you must userequire
(orrequire_relative
) first. - Repeated Loading:
require
only loads a file once. If you actually need to reload a file for debugging or dynamic purposes, you’d useload
. - Module vs. Class: You can’t use
include
on classes in the same manner as you can on modules (though you can include a module in a class). Classes are not typically “mixed in” to other classes.
- Using
A Quick Example to Illustrate Both
# lib/math_helpers.rb module MathHelpers def square(num) num * num end end # main.rb require_relative 'lib/math_helpers' # Load the file containing MathHelpers class Calculator include MathHelpers # Mix in methods from MathHelpers as instance methods end calc = Calculator.new puts calc.square(3) # => 9
require_relative 'lib/math_helpers'
loads themath_helpers.rb
file.include MathHelpers
makessquare
available as an instance method onCalculator
.
Conclusion
include
is about mixing a module’s methods into a class/module as instance methods. It’s essentially Ruby’s way of handling multiple inheritance for module functionality.require
(and its variantrequire_relative
) is how you load external files or libraries into your current Ruby program so you can use their classes, modules, or methods.
Use each keyword appropriately: include
for code organization and module reuse within your program, require
for pulling in external files and libraries.
Further Learning
To deepen your Ruby skill set and system design knowledge, check out these courses from DesignGurus.io:
- Grokking the Coding Interview: Patterns for Coding Questions – Master the coding patterns and algorithms crucial for interviews at top tech companies.
- Grokking System Design Fundamentals – Learn to architect scalable, distributed systems—valuable for building complex Ruby on Rails applications.
For personalized advice from ex-FAANG engineers, explore the Coding Mock Interview or System Design Mock Interview services.