Open In App

Ruby Hook Methods

Improve
Improve
Like Article
Like
Save
Share
Report

Ruby Hook Methods are called in reaction to something you do. They are usually used to extend the working of methods at run time. These methods are not defined by default, but a programmer can define them according to imply them on any object or class or module and they will come into picture when certain events occur. 

These methods help to extend the behavior of fundamentals when methods are called or subclasses of a class are formed or modules are incorporated. The meta programming ability of Ruby language helps the users to comfortably write dynamic codes at run time. 
The Hook Methods can perform a certain function once a particular action has been executed. 

There are several Ruby Hook Methods, but majorly, the followings have major roles to play: 

  1. Included
  2. Prepended
  3. Extended
  4. Inherited
  5. method_missing

Modules in Ruby
Before going through each of the methods, one needs to understand the concept of modules in Ruby. Modules are simply sets of code that could be written once and be used at multiple places. Usually, hook methods are used to access them and make changes in them. 

1. Included:
This method is used to include a method or attribute or module to another module. The method makes the underlined module available to the instances of the class. The following example explains the usage and working of the include method.

The example is a simple code to generate a comment when the module is executed in the class in which it is included. 

Ruby




# Declaring a module to greet a person
module Greetings
 
  def self.included(person_to_be_greeted)
 
    puts "The #{person_to_be_greeted} is welcomed with an open heart !"
  end
end
 
 
# Class where the module is included
class Person
 
  include Greetings # implementation of the include statement
end


Output: 

The Person is welcomed with an open heart !

2. Prepended:
This method was brought by Ruby 2.0. This is slightly different from what we observed above. Prepended method provides another way of extending the functioning of modules at different places. This uses the concept of overriding. The modules can be overridden using methods defined in the target class. 

The concept of Prepended method could be understood by the following self-explanatory example: 

Ruby




# Code as an example for prepend method
module Ruby
 
  def self.prepended(target)# Implementation of prepend method
    puts "#{self} has been prepended to #{target}"
  end
 
  def Type
    "The Type belongs to Ruby"
  end
end
 
class Coding
 
  prepend Ruby # the module Ruby is prepended
end
 
# Method call
puts Coding.new.Type


Output: 

Ruby has been prepended to Coding
The Type belongs to Ruby

3. Extended:
This method is a bit different from both the include and prepend method. While include applies methods in a certain module to instance of a class, extend applies those methods to the same class. 

The execution of the above mentioned code using the extend method can be done as follows: 

Ruby




# Code as  an example for extend method
module Ruby
 
  def self.extended(target)
    puts "#{self} was extended by #{target}"
  end
 
  def Type
    "The Type is Ruby"
  end
end
 
class Coding
 
  extend Ruby # Extending the module Ruby
end
 
# Method calling
puts Coding.Type


Output: 

Ruby was extended by Coding
The Type is Ruby

4. Inherited:
Inheritance as a concept is one of the most important concepts of Object Oriented Programming and is common in almost every programming language. In ruby, we deal in objects that are inspired from the real life, and thus, Oops operations play a very important role there. The inherited method is called whenever a subclass of a class is implemented. It is a method of making a child class from a parent class. 

The following example shows the same: 

Ruby




# Making the parent Vehicle class
class Vehicle
 
  def self.inherited(car_type)
    puts "#{car_type} is a kind of Vehicle"
  end
 
end
 
# Target class
class Hyundai < Vehicle #Inhereting the Vehicle class
end


Output: 

Hyundai is a kind of Vehicle

5. method_missing: 
method_missing method which is one of the most widely used in Ruby. This comes to action when one tries to call a method on an object that does not exist. 

The following example explains its working: 

Ruby




# The main class
class Ruby
 
  def method_missing(input, *args) # method_missing function in action
     "#{input} not defined on #{self}"
  end
 
  def Type
    "The Type is Ruby"
  end
end
 
var = Ruby.new
 
# Calling a method that exists
puts var.Type   
 
# Calling a method that does not exist
puts var.Name 


Output: 

The Type is Ruby
Name not defined on #<Ruby:0x0000000002363290> (object var)

There is a concept of callback that is often confused with the hook methods. While callbacks are the elements of the code of the program like methods, modules, etc, hook is just a spot in the code where they are accessed. Thus, the concept of callback should not be confused with hooks in Ruby. 

As we know, the Ruby language is one programming language that has a very clear correspondence with the daily life objects and methods, it is required that one who is working in the same, has thorough knowledge of all the necessary Oops concepts and Hook methods is one of them.

 



Last Updated : 13 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads