Ruby Academy · Lesson

What is Metaprogramming?

Understand how metaprogramming allows Ruby to modify its own structure at runtime.

Lesson 1 of 410 steps

What is Metaprogramming? is a free Ruby Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.

Understanding Metaprogramming

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.

What is Metaprogramming? — illustration 1

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.

Simple Metaprogramming Example

Using send, we can call methods dynamically.

class Person
  def greet
    'Hello!'
  end
end

p = Person.new
puts p.send(:greet)

Advantages of Metaprogramming

Metaprogramming allows for:

  • Less repetitive code (DRY principle).
  • More flexible and dynamic applications.
  • Easy modifications and enhancements at runtime.

Where is Metaprogramming Used?

Metaprogramming is used in:

  • Ruby on Rails (Active Record dynamically defines methods).
  • DSLs (Domain-Specific Languages).
  • Testing frameworks like RSpec.

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)

Using class_eval

class_eval dynamically modifies a class.

class Person
end

Person.class_eval do
  def greet
    'Hello!'
  end
end

puts Person.new.greet

Security Risks of Metaprogramming

Using metaprogramming can introduce security risks:

  • Executing unknown code with eval can be dangerous.
  • Overriding built-in methods can cause unexpected behavior.

Metaprogramming Recap

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.

What is Metaprogramming? — illustration 10
Free to start

Learn Ruby with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
29
Lessons
109

Frequently Asked Questions

Is the “What is Metaprogramming?” lesson free?

Yes — any 3 lessons of the Ruby Academy learning path, including “What is Metaprogramming?”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

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

  1. What is Metaprogramming?
  2. Dynamic Method Definition
  3. Open Classes and Monkey Patching
  4. Using send and eval
← Back to Ruby Academy