Ruby Methods: differences between load, require, include and extend in Ruby.

Small programs usually can be stored in one file so it won’t be a problem to read (and understand) them. Things start to complicate as your code grows: one day you may lose yourself in your own chaos and find it hard to organize your application. At this point, the best idea is to split your code (as a whole) into several files. To do that you need some helpful tricks that will let you use those files together. That's where the Ruby Methods come in.

Load vs require:

The load method simply reads and parses another files into your code every time a script is executed.

For example, if we have some module in one file...

test_module.rb

...and class in another...

test_class.rb

...then we can use the load 'test_module.rb' just before the class definition in test_class.rb file to get the access to the code contained in our module.

Another commonly used method is require. It reads the file from the file system, parses it, saves to the memory and runs it in a given place. What does it mean? Simply, even if you change the required file when the script is running, those changes won't be applied - Ruby will use the file from memory, not from the file system.

You can use it as follows: require 'test_module'

Note that we say filename not filename.rb mostly because not all extensions use files that ends with .rb

require gives you access to many libraries and extensions built in Ruby as well as many more stuff written by other programmers.

So when to use load? In most cases you’ll use require, but there are some cases when load is useful, for example, when your module changes frequently you may want to pick up those changes in classes that loads this module.

To summarize:

`require` reads and parses files only once, when they were referenced for the first time.

`load` reads and parses files every time you call `load`.

Include vs extend:

Files loaded by require or load will be parsed at the moment of loading, the code will be executed. To create reusable code, you've probably already defined a module or class that you would like to use elsewhere in your code. In order to do that you can choose one of the method explained below.

When you include a module into your class it behaves as if you took the code defined in your TestModule and inserted it in your TestClass. The include method is the way to “inject” code from one module into other modules or classes. It helps you DRY up your code - avoid duplications. For example, if we have many classes that would need same functionality we don’t have to write the same code in all of them. Instead, we can define the module and include it in any classes.

Output of the above code will be: “Some method of TestClass”

Unlike include, which adds module’s methods as instance methods, extend allows you to add them as a class methods.

The code above will print “Some method of TestClass”

As you can see, extend a TestModule inside TestClass will add the instance method - some_method to the extended class. include will add the instance methods - some_method to the instances of the TestClass

Summary

As you can see Ruby Methods may not only save your code, but also keep your work clean and transparent. The knowledge when to use methods in Ruby and what are the differences between the most popular cases like require, load, include and extend will certainly be helpful in your everyday coder life. Just to remember that the require and load are 'file-level' methods used to "read" and parse files, whereas include and extend are 'language-level' methods that can extend your class with other modules.

photo from: unsplash.com by Andrew Pons