ruby_cool_kid.rb — Meta Programming series: Dynamic dispatch
Tags: ruby, rails, metaprogramming
Published: 2021-09-21 21:56:20

Today, I want to start a new series of stories called: *“ruby_cool_kid.rb — Meta Programming series”*.

As of right now, I am reading a book called [“Metaprogramming Ruby 2: Program Like the Ruby Pros” by Paolo Perrotta](https://www.amazon.es/Metaprogramming-Ruby-Program-Like-Facets/dp/1941222129/ref=sr_1_1?__mk_es_ES=%C3%85M%C3%85%C5%BD%C3%95%C3%91&dchild=1&keywords=Metaprogramming+Ruby+2%3A+Program+Like+the+Ruby+Pros&qid=1632052597&sr=8-1), where he talks about meta programming in Ruby and its advantages.

As concepts in meta programming are abstract, and sometimes hard to grasp, I am starting this series to try and explain the concepts I learned in an easy way that can be easier understood. Also, it would work as a challenge for me to see if I understood what I learned.

*Please note that I will not be following Perrotta’s table of contents. I will follow my inspiration.*

...

After this short purpose introduction, let’s dive into it!

**Ruby**, as opposed to static programming languages like Java or C, **is a dynamic programming language.**

**Ruby is interpreted, not compiled**, meaning that it **can do at runtime what many static programming languages do at compilation**, for example, defining new classes or methods. You might be wondering, *what does dynamic dispatching have to do with all this.*

Well, **dynamic dispatching is the practice of dynamically calling a method based on data or information not known before.** The main idea that makes the engine work is the method `send` ([reference](https://apidock.com/ruby/Object/send)).

In Ruby, methods are not called on objects but rather method’s names are sent to a receiver, usually the current object.

As Alan Kay, one of the fathers of the idea of OOP used to say referring to OOP: *“The big idea is sending”* ([reference](https://softwareengineering.stackexchange.com/questions/264697/alan-kay-the-big-idea-is-messaging))

Based on this idea, `send` is a method that helps us call object methods without having to use *dot notation*

![1_SIaLAr2sweexc7OXDQoG8Q](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g28dknofe2uczeja6xe1.png)

Dynamic dispatching, takes advantage of this flexibility to call methods based on the data received. Let’s see an example.

Suppose you wanted to write a small program to congratulate your mom or dad on their birthday. We could write a little piece of code that would do so dynamically.

![1_N3NE49hVLv82ppRQ7qlUcQ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y00it7f1n6nalk0em8i1.png)

Of course, this is a very simple example that could have been done without dynamic dispatching, but it can be highly improved using Ruby’s tricks.

One of the goals or main definitions of meta programming is: **_Writing code that writes code_**, so, let’s try it. 

In the previous example, there is a lot of duplicated code, that can be simplified by using `define_singleton_method` ([reference](https://apidock.com/ruby/v2_5_5/Object/define_singleton_method)). It takes advantage of Ruby’s dynamic nature to define singleton methods. The code above is nice and works, but, what if we got confident and also wanted to congratulate our sister, our brother, our grandma? Let’s sharpen it: 

![1_K9dPtIQNQlr3i9H_GacbTw](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ini6l81ji2bytf9l56i.png)

As we can see, now `define_congratulations` is called whenever the `BirthdayCongratulations` class is instantiated. This method calls `define_singleton_method`, which takes a method’s name and a block and creates a method just for our object. Then, by using dynamic dispatching, we can call such a method. 

This has been a short concept example on how to use dynamic dispatching on Ruby. While it may not apply to all scenarios, it cannot be argued that it is a really powerful tool to have on our belt.

Also, take into account that despite being widely used by Ruby developers, send is not as good as it looks, as it exposes also private methods for the class. Use `public_send` ([reference](https://apidock.com/ruby/Object/public_send)) instead if you want to make sure your private methods remain private.

Did you think this code could not be further improved? It sure can! Follow me along for the next story *ruby_cool_kid.rb — Metaprogramming series: Ghost methods* 👻 where I will explain how missing methods can be found 😉

See you around!! 👋