Introduction
Ruby is a programming language that is very flexible. It enables developers to alter how the language works by itself. Functionality can be added to the core language features or even removed whenever required. It is also a highly portable, and cross-platform language.
So, in this article, we will elaborate on the concept of Hook methods in Ruby in detail.
What are Hook methods in Ruby?
Let us start by understanding the class, modules, and objects.
In Ruby, a class defines a set of methods that an object reacts to. Classes may extend or sub-class other classes, and override or inherit the methods of their super-class.
All objects are instances of a class.
A Module is a collection of class variables, constants, and methods. Modules are defined as a class, but with the module keyword not with the class keyword.
Module, Class, and Object implement several callback methods or hooks. These methods when defined for a class, module or object, get invoked when specific events occur.
The Hook Methods can perform a specific function once a particular action has been executed.
There are several Ruby Hook Methods, but the following are significant ones:
- Include
- Prepended
- Extended
- Inherited
- method_missing
Let us discuss these in detail.
Include
This method is used to include a module or method or attribute to another module. This specific method makes the underlined module available to the instances of the class.
Let us look at an example to understand the usage and working of the Include method.
module Example #Declaring the module
def self.included(name)
puts "Hi #{name}, This is Coding Ninjas!"
end
end
class User # Including module
include Example # Implementing the include statement
end
Output
Hi User, This is Coding Ninjas!
The above example generates a comment when the module is executed in the class in which it is included.
Prepended
The prepended method offers another way of extending the functioning of modules at different places. It uses the concept of overriding. The modules can be overridden with the methods defined in the target class.
This method came along with Ruby 2.0.
Let us look at one example to understand it in a better way:
module Coding
def self.prepended(target) #prepend method
puts "#{self} #{target}"
end
def Example
"Example for Prepend"
end
end
class Ninjas
prepend Coding #Coding module is prepended
end
puts Ninjas.new.Example #calling the method
Output
Coding Ninjas
Example for Prepend
Extended
The Extended method applies those methods to the same class, contrary to Include, which applies methods in a certain module to an instance of a class.
Extended is a bit different from both the Include and Prepend method.
An example is as follows:
module Coding
def self.extended(target)
puts "#{self} #{target}"
end
def Example
"This is Example"
end
end
class Ninjas
extend Coding #Coding module is extended
end
puts Ninjas.Example #calling method
Output
Coding Ninjas
This is Example
Inherited
The Inherited method is called when a subclass of a class is implemented. This is a method of creating a child class from a parent class.
This method basically helps in implementing one of the most important concepts of Object-Oriented Programming.
For example,
class Food #parent class
def self.inherited(food_type)
puts "#{food_type} is a kind of Food"
end
end
class Continental<Food #Inhereting the Food class
end
Output
Continental is a kind of Food
method_missing
Method_missing gets invoked when a method on an object is called that does not exist. It is one of the most used methods in Ruby.
The following example throws light on its working:
class One
def method_missing(input, *args) # method_missing function being used
"#{input} not defined on #{self}"
end
def Example
"This is Example"
end
end
var=One.new
puts var.Example #Calling existing method
puts var.Name #Calling non-existing method
Output:
This is Example
Name not defined on #<One:0x0000560ec7953220>