Peek under the hood: call methods by name with send, write methods at runtime with define_method, and catch unknown calls with method_missing.
The big picture
Code that writes code
Metaprogramming means the program treats its own methods as data — looking them up by name, building them on the fly, and fielding calls that were never defined.
1
Messages and Methods
Every call is a message sent to an object — Ruby just normally resolves it immediately.
2
send Calls by Name
obj.send(:name) invokes the method named by a Symbol you computed at runtime.
3
define_method Builds
define_method(:hi) { } installs a brand-new method while the program is running.
4
method_missing Catches
When nothing matches the message, method_missing gets one last chance to answer.
Code that writes code
Messages and Methods1Every call is a message sent to an object — Ruby just normally resolves it immediately.
send Calls by Name2obj.send(:name) invokes the method named by a Symbol you computed at runtime.
define_method Builds3define_method(:hi) { } installs a brand-new method while the program is running.
method_missing Catches4When nothing matches the message, method_missing gets one last chance to answer.
4 building blocks make up Code that writes code
Visualize
What happens to a method call
Follow one message from arrival to answer — including the safety net at the bottom.
process decision input / output start / end
Watch it run
send, method_missing and define_method
Program one dispatches by name; program two writes methods into a class while it runs.
dispatch.rb
1class Robot
2def say_hi
3"beep!"
4end
5
6defmethod_missing(name)
7"unknown: #{name}"
8end
9end
10
11bot = Robot.new
12puts bot.send(:say_hi)
13puts bot.send(:dance)
14puts bot.respond_to?(:say_hi)
main
class Robot gets one real method and one safety net.
Memory
no variables yet — press play
Console
output appears as the program runs…
Step 1/8
Practice
Match the metaprogramming kit
Click a term, then click its description to lock the pair.
5 conceptsTap a card to expand
?send(:hi)drops into place
?define_methoddrops into place
?method_missingdrops into place
?respond_to?drops into place
?Symboldrops into place
Challenge
Predict the output
A wizard answers every call — but respond_to? plays by different rules.
wizard.rb
1class Wizard
2defmethod_missing(name)
3"casting #{name}!"
4end
5end
6
7w = Wizard.new
8puts w.send(:fireball)
9puts w.respond_to?(:fly)
10puts w.fly
Your predicted output — one line per console line
Hint: respond_to? only reports defined methods — a method_missing fallback does not count. But a direct call like w.fly still lands in method_missing and gets a spell.
Recap
Key takeaways
Key takeaways5 takeaways
01
send(:name, args) calls a method whose name you only know at runtime.
02
define_method builds methods from data — loops over names replace copy-paste.
03
method_missing is the catch-all for messages nothing else handled.
04
respond_to? asks before calling — pair it with send for safe dispatch.
05
Rails dynamic finders and ActiveRecord attributes are powered by exactly these tools.