Skip to main content

dynamic_method_definitions

🛠️ Dynamic Method Creation with Lambdas

Use define_method alongside lambdas to generate methods on-the-fly, enabling DSLs or dynamic proxies. This pattern is powerful for metaprogramming libraries or when adapting to external schemas at runtime.

class APIClient
{get: :fetch, post: :create, delete: :remove}.each do |http_verb, method_name|
define_method(method_name) do |path, payload = {}|
-> { make_request(http_verb, path, payload) }.call
end
end

private
def make_request(verb, path, data)
# ... HTTP logic ...
end
end

client = APIClient.new
client.fetch("/users")