Monday, April 27, 2015

Ruby’s Important Hook Methods

Screenshot 2015-04-26 10.59.14

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 %Ruby’s Important Hook Methods%


by Imran Latif via SitePoint

No comments:

Post a Comment