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:
- Included
- Prepended
- Extended
- Inherited
- 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
module Greetings
def self .included(person_to_be_greeted)
puts "The #{person_to_be_greeted} is welcomed with an open heart !"
end
end
class Person
include Greetings
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
module Ruby
def self .prepended(target)
puts "#{self} has been prepended to #{target}"
end
def Type
"The Type belongs to Ruby"
end
end
class Coding
prepend Ruby
end
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
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
end
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
class Vehicle
def self .inherited(car_type)
puts "#{car_type} is a kind of Vehicle"
end
end
class Hyundai < Vehicle
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
class Ruby
def method_missing(input, *args)
"#{input} not defined on #{self}"
end
def Type
"The Type is Ruby"
end
end
var = Ruby. new
puts var.Type
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.