Ruby Metaprogramming
Shaher Shamroukh
Posted on May 23, 2021
What is Metaprogramming
Metaprogramming is one of those words that seems to exist to scare people.
However, it’s actually quite easy to understand if you look past the scary name.
So Are we talking about programming beyond programming?
Or Programming in the next dimension?
Actually, ruby metaprogramming is a set of coding techniques that allow us to get the results we need with less code.
Ruby support for metaprogramming starts by allowing our code to stay well informed about what’s going on around it.
With a little bit of effort, we can write Ruby programs that know when a new class is created, or when a method gets called, and even when the application is about to exit.
And of course, all this knowledge would be so much trivia if our program couldn’t do anything about it.
Fortunately, Ruby programs can do all sorts of things.
They can decide that there are still just a few details to take care of before the application exits.
they can even reprogram themselves. yes!
So Metaprogramming allows programs to create methods on the fly instead of having to define them in the program itself.
Basic Examples of Metaprogramming
We are going to start with an example from the Ruby on Rails framework, as it is one of the most popular open-source projects that use Ruby metaprogramming.
Imagine that we have created a database table for users that have columns such as name
email
etc.
And Of course, Rails doesn’t know what columns that we are going to have for the user database.
Then how exactly does the method find_by_email work when the method wasn’t actually defined by us or the framework?
Actually, Rails dynamically creates methods based on the column names in our database.
So the method is never defined, instead, it is generated on the fly by leveraging the metaprogramming technique.
Another Example of Metaprogramming is Monkey Patching
Yep, weird name I know!
Monkey patching is an object-oriented technique that allows us to open up classes and add or alter built-in behavior.
let's have an example with the string class
class String
def self.greet
"Hello Everyone!"
end
end
Now we can use this method on the String
class,
String.greet
=> "hello everyone!"
Here is a great article explaining monkey patching please check it out to have a better understanding of such a powerful technique, MonkeyPatching
Also, it's important to mention that Ruby provides us with many of powerful tools.
However, just because a tool is powerful, does not make it the right tool for the job. as it also provides us with tons of built-in methods ready for us to use.
Summary
This is just the tip of the iceberg.
Some of the most used gems in Rails were built in this way,
such as RSpec and ActiveRecord.
And having a good understanding of Ruby Metaprogramming is very helpful in understanding what those gems are doing exactly which makes us more productive and spend less time fixing bugs.
I hope this article can get you a bit closer to understanding metaprogramming and you have enjoyed the reading as I have enjoyed the writing.
Reference eloquentruby
Posted on May 23, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.