Back to blog

Blog

Unleashing Dynamic Method Creation in Ruby

Demystifying Ruby’s “define_method”

Jul 12, 2023 · dynamic-programming, metaprogramming, methods, ruby-on-rails, ruby
Unleashing Dynamic Method Creation in Ruby
Today I will talk about Meta Programming in Ruby, especially creating methods dynamically in a Ruby environment. In a simple definition, we can define meta programming as a technique by which you can write code that writes code by itself dynamically at runtime. If you like to learn more about the subject check out “Metaprogramming Ruby” book from here. For this article, we will focus only on how to create methods dynamically and we will discuss further concepts of meta programming related to Ruby in the future. Why would you want to create methods dynamically? There are a few good reasons. One would be maintaining high code quality. In a project, you would want to reduce similar methods which are slightly different and keep those more generic. Another one would be, it reduces the code and it will generate methods on runtime, which helps in code maintainability. Ultimately this would h...

The full article is published on Medium.

Read on Medium