proxy_method_missing
🔮 Building Dynamic Proxies with method_missing
and respond_to_missing?
​
Implement method_missing
alongside respond_to_missing?
to craft transparent proxies or delegators. This pattern underpins ORMs or RPC clients that map arbitrary method names to remote calls.
class RemoteProxy
def initialize(client)
@client = client
end
def method_missing(name, *args)
if name.to_s.start_with?('get_')
@client.call_api(name.to_s.sub('get_', ''), *args)
else
super
end
end
def respond_to_missing?(name, _)
name.to_s.start_with?('get_') || super
end
end
Always implement respond_to_missing?
to keep introspection libraries happy.