What is Metaprogramming?
Understand how metaprogramming allows Ruby to modify its own structure at runtime.
What is Metaprogramming? is a free Ruby Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
What is Metaprogramming?
Metaprogramming is the ability of Ruby to modify its own structure at runtime.
It allows developers to dynamically define methods, change class behavior, and manipulate objects in powerful ways.

2
Ruby's Dynamic Nature
Unlike statically typed languages, Ruby allows you to modify classes and objects dynamically.
Methods, variables, and even classes can be created and modified at runtime.
3
Simple Metaprogramming Example
Using send, we can call methods dynamically.
class Person
def greet
'Hello!'
end
end
p = Person.new
puts p.send(:greet)4
Advantages of Metaprogramming
Metaprogramming allows for:
- Less repetitive code (DRY principle).
- More flexible and dynamic applications.
- Easy modifications and enhancements at runtime.
5
Where is Metaprogramming Used?
Metaprogramming is used in:
- Ruby on Rails (Active Record dynamically defines methods).
- DSLs (Domain-Specific Languages).
- Testing frameworks like RSpec.
6
Using instance_eval
instance_eval allows executing code in the context of an object.
class Person
end
p = Person.new
p.instance_eval { @name = 'Alice' }
puts p.instance_variable_get(:@name)7
Using class_eval
class_eval dynamically modifies a class.
class Person
end
Person.class_eval do
def greet
'Hello!'
end
end
puts Person.new.greet8
9
Security Risks of Metaprogramming
Using metaprogramming can introduce security risks:
- Executing unknown code with
evalcan be dangerous. - Overriding built-in methods can cause unexpected behavior.
10
Congratulations!
You have learned what metaprogramming is and how it allows dynamic modifications in Ruby.
Next, you will explore how to define and call methods dynamically.

Frequently asked questions
Is the “What is Metaprogramming?” lesson free?
Yes — the full text of “What is Metaprogramming?” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.
What will I learn in “What is Metaprogramming?”?
Understand how metaprogramming allows Ruby to modify its own structure at runtime. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ruby Academy?
No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “What is Metaprogramming?” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ruby Academy lesson?
Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- What is Metaprogramming?
- Dynamic Method Definition
- Open Classes and Monkey Patching
- Using send and eval