# Things to Consider when Metaprogramming in Ruby

DevFeed: [Things to Consider when Metaprogramming in Ruby](<https://devfeed.tech/articles/things-to-consider-when-metaprogramming-in-ruby-21071.md>)

Original publisher: [Read original article](<https://jakeyesbeck.com/2016/04/10/things-to-consider-when-metaprogramming-in-ruby/>)

Published: 2016-04-10T12:00:00Z

Content type: article

Language: en

Sources: [Jake Yesbeck](<https://devfeed.tech/sources/jake-yesbeck.md>)

Topics: [Ruby](<https://devfeed.tech/topics/ruby.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [developers](<https://devfeed.tech/tags/developers.md>), [examples](<https://devfeed.tech/tags/examples.md>), [performance](<https://devfeed.tech/tags/performance.md>), [readability](<https://devfeed.tech/tags/readability.md>), [ruby](<https://devfeed.tech/tags/ruby.md>)

## AI overview

An overview of Ruby metaprogramming that examines its benefits, costs, effects on code discovery and readability, and performance considerations in frequently executed code.

## Source excerpt

Metaprogramming in Ruby is a polarizing topic. The most common purpose of Ruby metaprogramming is for code to alter itself at runtime. Metaprogramming can be used to achieve terse and more flexible code. However, it is not without its cost. As with most things, nothing of value is free, even metaprogramming. Undoubtedly, there is a time and place for metaprogramming; but, awareness of concessions that need to be made to support a metaprogrammed solution is important. Code Discovery and Readability One problem with metaprogramming solutions are their obstruction of code discovery. When entering a new project or simply trying to re-familiarize oneself with an existing one, tracing code execution in a text editor can be quite difficult if method definitions do not exist. For example, we can assume that a User class exists with a set of metaprogrammed methods: class User [ :password, :email, :first_name, :last_name ].each do |attribute| define_method(:"has_#{attribute}?") do self.send(attribute).nil? end end end Although a little contrived, this code is a list of simple convenience methods on a User class. This solution is easily extended to include additional attributes without a full method definition per attribute. However, these methods can not be found using grep, silver searcher, or other "find all" tools. Since the method has_password? is never explicitly defined in the code, it is not discoverable. A Work Around: To combat this issue, some developers choose to write a comment listing the defined method names above the metaprogramming block. This simple solution can greatly help the readability of the code: class User # has_password?, has_email?, has_first_name?, # has_last_name? method definitions [ :password, :email, :first_name, :last_name ].each do |attribute| define_method(:"has_#{attribute}?") do self.send(attribute).nil? end end end Performance Depending on the amount of times a piece of code is executed, performance considerations can be extremely importa