|
| 1 | +--- |
| 2 | +title: "Ruby Metaprogramming: Writing Code that Writes Code" |
| 3 | +date: 2025-09-11 09:15:00 -0800 |
| 4 | +categories: [Programming, Ruby] |
| 5 | +tags: [ruby, metaprogramming, dynamic, reflection] |
| 6 | +author: dain |
| 7 | +--- |
| 8 | + |
| 9 | +Metaprogramming is one of Ruby's most powerful features, allowing you to write code that manipulates code itself at runtime. It's what makes Ruby so flexible and dynamic. |
| 10 | + |
| 11 | +## What is Metaprogramming? |
| 12 | + |
| 13 | +Metaprogramming is the practice of writing programs that can read, generate, analyze, or transform other programs, and even modify themselves while running. |
| 14 | + |
| 15 | +## Dynamic Method Definition |
| 16 | + |
| 17 | +You can define methods dynamically using `define_method`: |
| 18 | + |
| 19 | +```ruby |
| 20 | +class Person |
| 21 | + %w[name age email].each do |attribute| |
| 22 | + define_method attribute do |
| 23 | + instance_variable_get("@#{attribute}") |
| 24 | + end |
| 25 | + |
| 26 | + define_method "#{attribute}=" do |value| |
| 27 | + instance_variable_set("@#{attribute}", value) |
| 28 | + end |
| 29 | + end |
| 30 | +end |
| 31 | + |
| 32 | +person = Person.new |
| 33 | +person.name = "John" |
| 34 | +person.age = 30 |
| 35 | +puts person.name # => "John" |
| 36 | +``` |
| 37 | + |
| 38 | +## Method Missing |
| 39 | + |
| 40 | +The `method_missing` hook allows you to respond to undefined method calls: |
| 41 | + |
| 42 | +```ruby |
| 43 | +class MagicHash |
| 44 | + def initialize |
| 45 | + @data = {} |
| 46 | + end |
| 47 | + |
| 48 | + def method_missing(method_name, *args) |
| 49 | + if method_name.to_s.end_with?('=') |
| 50 | + # Setter method |
| 51 | + key = method_name.to_s.chomp('=') |
| 52 | + @data[key] = args.first |
| 53 | + else |
| 54 | + # Getter method |
| 55 | + @data[method_name.to_s] |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + def respond_to_missing?(method_name, include_private = false) |
| 60 | + true |
| 61 | + end |
| 62 | +end |
| 63 | + |
| 64 | +hash = MagicHash.new |
| 65 | +hash.username = "johndoe" |
| 66 | + |
| 67 | +puts hash.username # => "johndoe" |
| 68 | +``` |
| 69 | + |
| 70 | +## Class Macros |
| 71 | + |
| 72 | +Ruby's `attr_accessor`, `attr_reader`, and `attr_writer` are examples of class macros: |
| 73 | + |
| 74 | +```ruby |
| 75 | +class Product |
| 76 | + attr_accessor :name, :price |
| 77 | + attr_reader :id |
| 78 | + |
| 79 | + def initialize(id, name, price) |
| 80 | + @id = id |
| 81 | + @name = name |
| 82 | + @price = price |
| 83 | + end |
| 84 | +end |
| 85 | +``` |
| 86 | + |
| 87 | +You can create your own class macros: |
| 88 | + |
| 89 | +```ruby |
| 90 | +class ActiveRecord |
| 91 | + def self.validates_presence_of(*attributes) |
| 92 | + attributes.each do |attribute| |
| 93 | + define_method "validate_#{attribute}" do |
| 94 | + if instance_variable_get("@#{attribute}").nil? |
| 95 | + raise "#{attribute} cannot be nil" |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | +end |
| 101 | + |
| 102 | +class User < ActiveRecord |
| 103 | + validates_presence_of :email, :password |
| 104 | + |
| 105 | + attr_accessor :email, :password |
| 106 | +end |
| 107 | +``` |
| 108 | + |
| 109 | +## Module Inclusion Hooks |
| 110 | + |
| 111 | +Ruby provides hooks that are called when modules are included: |
| 112 | + |
| 113 | +```ruby |
| 114 | +module Trackable |
| 115 | + def self.included(base) |
| 116 | + base.extend(ClassMethods) |
| 117 | + base.class_eval do |
| 118 | + attr_accessor :created_at, :updated_at |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + module ClassMethods |
| 123 | + def track_creation |
| 124 | + define_method :initialize do |*args| |
| 125 | + super(*args) |
| 126 | + @created_at = Time.now |
| 127 | + @updated_at = Time.now |
| 128 | + end |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | + def touch |
| 133 | + @updated_at = Time.now |
| 134 | + end |
| 135 | +end |
| 136 | + |
| 137 | +class Article |
| 138 | + include Trackable |
| 139 | + track_creation |
| 140 | + |
| 141 | + attr_accessor :title, :content |
| 142 | +end |
| 143 | +``` |
| 144 | + |
| 145 | +## Eval Family |
| 146 | + |
| 147 | +Ruby provides several `eval` methods for dynamic code execution: |
| 148 | + |
| 149 | +```ruby |
| 150 | +# eval - executes a string as Ruby code |
| 151 | +code = "puts 'Hello from eval!'" |
| 152 | +eval(code) |
| 153 | + |
| 154 | +# instance_eval - executes code in the context of an object |
| 155 | +person = Person.new |
| 156 | +person.instance_eval do |
| 157 | + @name = "Alice" |
| 158 | + @age = 25 |
| 159 | +end |
| 160 | + |
| 161 | +# class_eval - executes code in the context of a class |
| 162 | +Person.class_eval do |
| 163 | + def greet |
| 164 | + "Hello, I'm #{@name}" |
| 165 | + end |
| 166 | +end |
| 167 | +``` |
| 168 | + |
| 169 | +## Best Practices |
| 170 | + |
| 171 | +1. **Use metaprogramming sparingly** - It can make code hard to understand and debug |
| 172 | +2. **Prefer explicit over implicit** - Clear code is better than clever code |
| 173 | +3. **Document your metaprogramming** - Future you will thank you |
| 174 | +4. **Test thoroughly** - Dynamic code is harder to catch with static analysis |
| 175 | + |
| 176 | +Metaprogramming is a double-edged sword. Use it wisely to create elegant, DRY code, but don't sacrifice readability for cleverness. |
0 commit comments