Ruby Hook Methods
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:
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.
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.
The Person is welcomed with an open heart !
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 has been prepended to Coding The Type belongs to Ruby
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 was extended by Coding The Type is Ruby
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:
Hyundai is a kind of Vehicle
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:
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.