Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction  
2.
What are Hook methods in Ruby? 
2.1.
Include
2.2.
Prepended
2.3.
Extended
2.4.
Inherited
2.5.
method_missing
3.
Frequently Asked Questions
3.1.
Are callbacks the same as hook methods in Ruby?
3.2.
Which websites have been developed using Ruby?
3.3.
Who developed Ruby?
4.
Conclusion  
Last Updated: Mar 27, 2024

Hooks in Ruby

Author Vidhi Singh
0 upvote

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. 

Hooks in Ruby

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: 

  1. Include
  2. Prepended
  3. Extended
  4. Inherited
  5. 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 
You can also try this code with Online Ruby Compiler
Run Code

 

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 
You can also try this code with Online Ruby Compiler
Run Code

 

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
You can also try this code with Online Ruby Compiler
Run Code

 

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
You can also try this code with Online Ruby Compiler
Run Code

 

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
You can also try this code with Online Ruby Compiler
Run Code

 

Output:

This is Example
Name not defined on #<One:0x0000560ec7953220>

Frequently Asked Questions

Are callbacks the same as hook methods in Ruby?

Callbacks are the parts of the code of the program like modules, methods, etc, whereas a hook is just a spot in the program where they are accessed. Therefore, in Ruby, the concept of the callback is different from hooks. 

Which websites have been developed using Ruby?

A few websites are Airbnb, Kickstarter, Github, etc.

Who developed Ruby?

Ruby was developed by a Japanese computer programmer- Yukihiro “Matz” Matsumoto. 

Conclusion  

This article discusses Hooks in Ruby. You can read more about this language here
Further, if you have any doubts, you can refer to the official FAQ page of Ruby. 

To be an expert in Ruby, we recommend you to check out the Coding Ninjas Ruby Courses and Articles

Do upvote our blog and help other ninjas grow!

Happy Coding!

Live masterclass