

Metaprogramming: RUBY HOOK METHODS
source link: https://www.tuicool.com/articles/hit/R3Mn63m
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.


Metaprogramming: Ruby Hook Methods
Ruby comes with a bunch of hook methods that allow you to manipulate classes, modules and objects on the fly.
Here is a list of the most important hook methods:
Module#included Module#extended Module#prepended Class#inherited
included, extended and prepended modules
These hook methods are invoked whenever a module is included, extended or prepended in another module or class.
They work pretty similarly. So here we’re going to detail the included
hook method
produces
The MediumPost entity now accepts comments !
This hook method allows you to add methods and attributes according to the class/module that includes the module where the hook method is defined.
This mechanism is actually used by the ActiveSupport::Concern
module.
The implementation is pretty similar for the Module#extended
and Module#prepended
hook methods.
Class#inherited
This hook method is called whenever a subclass of the class that implements the hook method is created
produces
Tenant is a kind of user
This hook method is pretty handy when you want to define a variable or a class_attribute
at class definition for each children.
That’s exactly what the ActiveRecord::Core
module does with the initialize_find_by_cache
class method.
Conclusion
Hook methods can be pretty handy for class and module manipulation on the fly.
However, as they modify the module/class blueprint, it can provoke some undesired side effects like superclass method overriding, etc..
I’ll detail the BasicObject#method_missing
hook method in another article.
Voilà !
Thank you for taking the time to read this post :-)
Feel free to :clap: and share this article if it has been useful for you.
Here is a link to my last article: Private & Protected: a matter of Message
.
Recommend
-
32
The latest release of Rust, version 1.30, extends procedural macros by allowing them to define new attributes and function-like macros. Additionally...
-
33
In Karumi over the last few months we have been trying to generate a Sourcery template that would help us to automate the composition root file we usually have to deal with all the dependencies generation ceremony, but du...
-
46
What is Manifold? Manifold re-energizes Java with powerful features like Type-safe M...
-
21
README.md
-
24
The art of metaprogramming, Part 1 Introduction to metaprogramming Write programs to generate other programs Jonathan Bartlett Published on October 20, 2005 ...
-
33
In some domains of programming it’s common to want to write a data structure or algorithm that can work with elements of many different types, such as a generic list or a sorting algorithm that only needs a comparison fu...
-
44
It's hard to avoid using Devise in a Rails project. That's mostly because the ease of using it is hard to beat. Devise is incredibly configurable and that often means having code that can be hard to follow. Providi...
-
10
Hello children and parents, ahem Rubyists. In an earlier article, we dove into the ancestry chain. In today’s post, we’ll dive deeper into parenting and inheritance. We w...
-
4
Metaprogramming in Ruby: Part 2 September 18, 2015 Table of Contents Metaprogramming in...
-
6
An Introduction to Metaprogramming in RubyDaniel Lempesis on Jul 26, 2023
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK