Nowadays, a lot of programmers are switching to Ruby language for its awesomeness. Most of them are having the problem of adopting some concepts of Ruby language. That’s why today I am writing on some basics of Ruby language. Basically, I will clear the concept among load, require, include and extend and how they are related to ruby module and mix-ins.
At first, I will talk about Module and Mix-ins. Then, I will try to relate with keywords: include, extend, load and require. Like other languages, the namespace concept is also existing in Ruby programming language. One of the main usages of module is namespace. Normally, namespace is used to group up the same type of classes.
For example:
module Flyer class Bird end class Plane end end |
Here, both of classes: Bird and Plane are wrapped by namespace: Flyer. Now, you can use them in following way…
bird = Flyer::Bird.new plane = Flyer::Plane.new |
Now, I will show a little code snippet..
class Bird def fly "I can fly." end end class Plane Def fly "I can fly." end end |
Yeah, both of the classes have similar types of methods. So, if you want to modify the method, you will have to go through all of them. But, this goes against the DRY (Don’t Repeat Yourself) principle. And, Ruby also doesn’t support the multiple inheritance. So, if you want to horizontally expand your class, ruby has a nice concept called mix-inx. Basically, by this concept you can include a module into a class.
module Flyer Def fly "I can fly." end end |
include
The concept of include here it is. By “include”, you can add an extra instance method into a class.
class Bird include Flyer ---------------------- Other implementations ---------------------- end |
So, now
bird = Bird.new puts bird.fly #=> It will basically return the string: I can fly. |
extend
You need “extend” when you want to add class method into a class. Let say, you have a module
module Animal def eat "Yeah! I can eat." end end |
Now, you want to add this “eat” method to your Bird class
class Bird extend Animal ---------------------- Other implementations ---------------------- end puts Bird.eat #=> It will basically return the string: Yeah! I can eat. |
Just one thing, you don’t have to use the keyword: self in the module. If you just extend it, it will be class method automatically.
require
Now, I will explain the “require”. We have already include and extend to load intance and class methods. So, why do we need more key words. Here is the catch… If you want to load a library of different file name, then you need keywords: require or load in order to load the file first. Require laods a file only once. Require return false when you try to load same file multiple times. Let’s look at an exmale. Suppose, your flyer module is in the file: abc.rb, then you will have to load the abc.rb file by “require”. Look at the example….
requre 'abc' class Bird include Flyer ---------------------- Other implementations ---------------------- end |
Just require ‘abc’ is okay. You don’t have use “.rb” extension. Require normally searches for files in the locations deifned in “LOAD_PATH”. You will get all defined paths by global variables “$LOAD_PATH”. You can also add your file’s directory path in $LOAD_PATH.
$LOAD_PATH << File.dirname(__FILE__) OR, $LOAD_PATH << File.dirname('your folder location') |
load
Load works same as “require” does but it loads the same file multiple times. If you file’s state gets changed, you can load the file by “load” keyword multiple times.
load "abc.rb" |
For “load”, you will have to mention file name with its extension like “.rb”. Done 🙂