Ruby Hook Methods - GeeksforGeeks

13 Aug, 2021
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: 
IncludedPrependedExtendedInheritedmethod_missing
Included
Prepended
Extended
Inherited
method_missing
Modules in RubyBefore 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 personmodule Greetings   def self.included(person_to_be_greeted)     puts "The #{person_to_be_greeted} is welcomed with an open heart !"  endend  # Class where the module is includedclass Person   include Greetings # implementation of the include statementend
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 methodmodule Ruby   def self.prepended(target)# Implementation of prepend method    puts "#{self} has been prepended to #{target}"  end   def Type    "The Type belongs to Ruby"  endend class Coding   prepend Ruby # the module Ruby is prependedend # Method callputs 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 methodmodule Ruby   def self.extended(target)    puts "#{self} was extended by #{target}"  end   def Type    "The Type is Ruby"  endend class Coding   extend Ruby # Extending the module Rubyend # Method callingputs 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 classclass Vehicle   def self.inherited(car_type)    puts "#{car_type} is a kind of Vehicle"  end end # Target classclass Hyundai < Vehicle #Inhereting the Vehicle classend
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 classclass Ruby   def method_missing(input, *args) # method_missing function in action     "#{input} not defined on #{self}"  end   def Type    "The Type is Ruby"  endend var = Ruby.new # Calling a method that existsputs var.Type    # Calling a method that does not existputs 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.
 
2000sparshgoyal
Picked
Ruby Keyword
Ruby-OOP
Ruby
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Ruby | Array count() operation
Include v/s Extend in Ruby
Ruby | Enumerator each_with_index function
Global Variable in Ruby
Ruby | Array select() function
Ruby | Case Statement
Ruby | Hash delete() function
Ruby | Data Types
Ruby | Decision Making (if, if-else, if-else-if, ternary) | Set - 1
Ruby | String gsub! Method