From a8174251f17ba8e98f70a7099b8150e80cf19cb2 Mon Sep 17 00:00:00 2001 From: Eric Proulx Date: Sat, 19 Jul 2025 14:52:20 +0200 Subject: [PATCH] Replace methods by singleton_methods when overriding them. --- CHANGELOG.md | 1 + lib/grape/api.rb | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd24c4ef6..93aa18eda 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * [#2581](https://github.com/ruby-grape/grape/pull/2581): Delegate `to_s` in Grape::API::Instance - [@ericproulx](https://github.com/ericproulx). * [#2582](https://github.com/ruby-grape/grape/pull/2582): Fix leaky slash when normalizing - [@ericproulx](https://github.com/ericproulx). * [#2583](https://github.com/ruby-grape/grape/pull/2583): Optimize api parameter documentation and memory usage - [@ericproulx](https://github.com/ericproulx). +* [#2585](https://github.com/ruby-grape/grape/pull/2585): Optimize mount methods overriding - [@ericproulx](https://github.com/ericproulx). * Your contribution here. #### Fixes diff --git a/lib/grape/api.rb b/lib/grape/api.rb index 81d101abb..3640d7e93 100644 --- a/lib/grape/api.rb +++ b/lib/grape/api.rb @@ -5,7 +5,7 @@ module Grape # should subclass this class in order to build an API. class API # Class methods that we want to call on the API rather than on the API object - NON_OVERRIDABLE = %i[call call! configuration compile! inherited recognize_path].freeze + NON_OVERRIDABLE = %i[call call! configuration compile! inherited recognize_path to_s].freeze Helpers = Grape::DSL::Helpers::BaseHelper @@ -51,7 +51,7 @@ def initial_setup(base_instance_parent) # Redefines all methods so that are forwarded to add_setup and be recorded def override_all_methods! - (base_instance.methods - Class.methods - NON_OVERRIDABLE).each do |method_override| + (base_instance.singleton_methods - NON_OVERRIDABLE).each do |method_override| define_singleton_method(method_override) do |*args, &block| add_setup(method: method_override, args: args, block: block) end