Ruby's Important Hook Methods

Originally published at:

Ruby’s philosophy is based on a strong primitive which is programmer happiness. Ruby strongly believes in programmer happiness and it has provided many different ways to make it so. Its metaprogramming capabilities allow programmers to write dynamic code at runtime. It’s threading abilities give programmers an elegant way to write multithreaded code. It’s hook methods help programmers extend behavior of programs at runtime.

The aforementioned features, along with some other cool language aspects, make Ruby one of the preferred choices for writing code. This post will explore some important hook methods in Ruby. We will discuss different aspects about hook methods, such as what they are, what are they used for, and how we can use them to solve different problems. We will also look at how popular Ruby frameworks/gems/libraries use them to provide pretty cool features.

Let’s get started.

What is a Hook Method?

Hook methods provide a way to extend behavior of programs at runtime. Imagine having the ability to get notified whenever a child class inherits from some particular parent class or handling non-callable methods on objects elegantly without allowing the compiler to raise exceptions. These are some of the use cases for hook methods, but their usage is not limited to this. Different frameworks/libraries have used different hook methods to achieve their desired functionality.

We will be discussing following hook methods in this post:

  • included
  • extended
  • prepended
  • inherited
  • method_missing

Continue reading this article on SitePoint


Nice Article. Good Work!

Thanks for liking :thumbsup: .

Indeed a very well versed article, on the mysteries of hook methods in ruby. Learned allot from the article and will be looking to next article by author.

Thanks for liking my article. Stay tuned for upcoming articles.

Good job… really well explained.

Thanks for liking :smile:. Stay tuned for next articles.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.