Introduction
In this article, we will learn about loading and requiring Modules in the ruby programming language.
A Ruby module is a bundle of constants and methods. Module methods or instance methods can both be used in modules. Methods in a class that is included in a module are known as instance methods. While instance methods cannot be called without first building an encapsulating object, module methods can.
Loading and Requiring Modules
Ruby programs can be divided up into several files, and the most logical method to do this is to put each nontrivial class or module into its own file. Then, using require or load, these different files can be combined into a single program (and, if it is well-designed, it can be reused by many other applications). While they are used like language keywords, these are global functions that are specified in the kernel. The standard library is also used to load files using the same required approach.
Although require is considerably more frequently used than load, load and require both have similar functions. Both routines have the ability to load and run a Ruby source file. The specific file is loaded if the path to the one being loaded is either absolute or relative to ~ (the user's home directory). The file is typically supplied as a relative path, and both load and require look for it in relation to the directories in Ruby's load path.
Despite these general similarities, load and require differ significantly in key ways.
- Require can load binary extensions to Ruby in addition to source code. Although binary extensions depend on the implementation, they often appear as shared library files with extensions like .so or.dll in C-based implementations.
- load anticipates a whole filename with an extension. Instead of a filename, require is typically provided a library name without an extension. If yes, it looks for a file with the base name of the library and the relevant source or native library extension. Require will load the source file rather than the binary file if a directory contains both an.rb source file and a file with a binary extension.
Ruby programs are also capable of modifying their own load path, usually by altering the components of the $LOAD_PATH array. Let’s see a few examples to understand this:
# Removing the currently used directory from the load path.
$:.pop if $:.last == '.'
# Adding the installation directory for the current application
# to the beginning of that load path.
$LOAD_PATH.unshift File.expand_path($PROGRAM_NAME)
# Add that value of an environment variable to the end of the path.
$LOAD_PATH << ENV['MY_LIBRARY_DIRECTORY']
Finally, keep in mind that you can bypass the load path entirely by passing absolute filenames (that begin with / or ~) to load or require.
The Load Path
Ruby’s load path is like an array that can be accessed using either of the global variables, $: or $LOAD_PATH. Ruby will look in each directory listed in the array's elements for files to load. The array's beginning directories are searched before the array's ending directories. In Ruby 1.8, the components of $LOAD_PATH had to be strings, but in Ruby 1.9, they may also be other objects with to_path methods that return strings.
Your Ruby implementation, the operating system it runs on, and even where in your filesystem you installed it all affect the default value of $LOAD PATH.
With ruby -e "puts $:", you may get a typical value for Ruby 1.8 like this:
/usr/lib/site_ruby/1.8
/usr/lib/site_ruby/1.8/i386-linux
/usr/lib/site_ruby
/usr/lib/ruby/1.8
/usr/lib/ruby/1.8/i386-linux
In the Ruby 1.9 version, the default load path is way more complicated. Let's see is a typical value to understand this:
/usr/local/lib/ruby/gems/1.9/gems/rake-0.7.3/lib /usr/local/lib/ruby/gems/1.9/gems/rake-0.7.3/bin
/usr/local/lib/ruby/site_ruby/1.9
/usr/local/lib/ruby/site_ruby/1.9/i686-linux
/usr/local/lib/ruby/site_ruby
/usr/local/lib/ruby/vendor_ruby/1.9
/usr/local/lib/ruby/vendor_ruby/1.9/i686-linux
/usr/local/lib/ruby/vendor_ruby
/usr/local/lib/ruby/1.9
/usr/local/lib/ruby/1.9/i686-linux
Executing Loaded Code
Both require and load execute the code within the specified file almost immediately. However, calling these methods is not the same as merely substituting the code included in the file for the calls to load or require.
The top-level scope in which files loaded with load or require are executed is new and distinct from the top-level scope in which load or require was used. The loaded file has access to all globally specified variables and constants at the moment of loading, but it is not able to access the local scope from which the load was started.
Autoloading Modules
The autoload methods of Module and Kernel are used to allow lazy loading of the files on the appropriate basis. You can register the name of an undefined constant (usually a class or module name) and the name of the library that defines it using the global autoload function.
The named library is loaded by require when that constant is first referenced.
Let’s see an example to understand this:
# Require 'socket' if and when that 'TCPSocket' is first used.
autoload :TCPSocket, "socket"
To interact with constants nested within another module, the Module class defines its own version of autoload.
Frequently Asked Questions
Why are modules used in Ruby?
Modules can be understood as a way of grouping together methods, constants, and classes. Modules give you two main benefits. Modules prevent name clashes and provide a namespace. Modules implement the mixin facility.
How does a module work in Ruby?
Simply put, a Ruby module is a collection of objects with the same name. Constants, methods, classes, or other modules could be the objects. The uses of modules are two. A module is a handy way to group items together, or you can use the include command in Ruby to include its contents in a class.
How do Ruby classes work?
In Ruby, a class is like an object that defines a blueprint to create other objects. Classes define which methods are available in any instance of that particular class. When we define a method inside a class, it creates an instance method on that particular class. Any future instance of that particular class will have that method available.
When should you use a class in Ruby?
Generally, a class should be used for the functionality that needs instantiation or that needs to keep track of states. A module can be utilized either as a way for mixing functionality into multiple classes or as a way for providing one-off features that don't need to be instantiated or to keep track of state.
Differentiate between modules and classes?
A class is more like a unit, and a module is like a loose collection of stuff like functions, classes, or variables. In a public module, the classes in that particular project have access to all the functions and variables of that module. We don't have to specify the module name to address one.