Skip to main content

metaprogramming_with_define_method

✨ Using Lambdas in Metaprogramming and DSLs

In DSLs or dynamic APIs, inject lambdas into define_method calls to create methods with captured context. This technique builds fluent interfaces and reduces boilerplate significantly.

class ModelBuilder
def initialize(&block)
instance_eval(&block)
end

def field(name, &transform)
define_singleton_method(name) do |value|
transform ? transform.call(value) : value
end
end
end

user = ModelBuilder.new do
field(:upcase) { |v| v.upcase }
field(:double) { |v| v * 2 }
end

puts user.upcase("hello") # => "HELLO"
puts user.double(5) # => 10

Bind service locators or policy checkers via closures for high-level separation of concerns.