Metaprogramming Introduction in Ruby
In this lesson, we are going to learn about metaprogramming in Ruby. Starting off with a practical example.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this lesson we are going to start learning about metaprogramming in Ruby. We'll begin with a practical example.


This is a profile page for You can see my full name listed on the top right hand side. This name was collected by the application when I signed up, and when I logged in later, it was retrieved from the database and displayed.

To display this value, I can update my User class in the application and override the built in String to_s method, like so:

def to_s
  self.first_name + " " + self.last_name

Now, if I call the to_s method on any objects created from the User class we'll get this full name value.

So, metaprogramming gives us the ability to open up classes and override existing methods to give applications custom behavior. This is by no means the full scope of what metaprogramming offers, but it's a introduction to how you can easily use it in your applications.