FIX: Make MiniProfiler.profile_method compatible with Ruby 3 (#481)

FIX: Make MiniProfiler.profile_method compatible with Ruby 3 (#481)

  • Make MiniProfiler.profile_method compatible with Ruby 3

Signed-off-by: OsamaSayegh asooomaasoooma90@gmail.com

  • use ruby2_keywords hack

Signed-off-by: OsamaSayegh asooomaasoooma90@gmail.com

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index cea23cb..8d426ed 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -21,11 +21,11 @@ jobs:
         options: --health-cmd "timeout 5 bash -c 'cat < /dev/null > /dev/udp/127.0.0.1/11211'" --health-interval 10s --health-timeout 5s --health-retries 5
     strategy:
       matrix:
-        ruby: ["2.7", "2.6", "2.5"]
+        ruby: ["3.0", "2.7", "2.6", "2.5"]
         redis: ["5.x"]
     steps:
       - uses: actions/checkout@v2
-      - uses: actions/setup-ruby@v1
+      - uses: ruby/setup-ruby@v1
         with:
           ruby-version: ${{ matrix.ruby }}
       - name: Setup redis
diff --git a/lib/mini_profiler/profiling_methods.rb b/lib/mini_profiler/profiling_methods.rb
index 306cd61..e62f129 100644
--- a/lib/mini_profiler/profiling_methods.rb
+++ b/lib/mini_profiler/profiling_methods.rb
@@ -115,6 +115,9 @@ module Rack
             end
           end
         end
+        if klass.respond_to?(:ruby2_keywords, true)
+          klass.send(:ruby2_keywords, with_profiling)
+        end
         klass.send :alias_method, method, with_profiling
       end
 
@@ -154,7 +157,6 @@ module Rack
       def clean_method_name(method)
         method.to_s.gsub(/[\?\!]/, "")
       end
-
     end
   end
 end
diff --git a/spec/lib/profiler_spec.rb b/spec/lib/profiler_spec.rb
index dc582d4..eabe33e 100644
--- a/spec/lib/profiler_spec.rb
+++ b/spec/lib/profiler_spec.rb
@@ -55,6 +55,10 @@ describe Rack::MiniProfiler do
         [bar, baz, yield]
       end
 
+      def kwargs_test(a, b, c = 1, d: 4)
+        { a: a, b: b, c: c, d: d }
+      end
+
       def self.bar(baz, boo)
         [baz, boo, yield]
       end
@@ -76,6 +80,14 @@ describe Rack::MiniProfiler do
       Rack::MiniProfiler.unprofile_singleton_method TestClass, :bar
     end
 
+    it 'optional positional args and keyword args should not conflict' do
+      block_args = nil
+      Rack::MiniProfiler.profile_method(TestClass, :kwargs_test) do |a, b, c = 1, d: 4|
+        block_args = { a: a, b: b, c: c, d: d }
+      end
+      expect(TestClass.new.kwargs_test(10, 20, d: 90)).to eq({ a: 10, b: 20, c: 1, d: 90 })
+      expect(block_args).to eq({ a: 10, b: 20, c: 1, d: 90 })
+    end
   end
 
   describe 'step' do

GitHub sha: 1994a3d0

This commit appears in #481 which was merged by SamSaffron.