Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

find-method does not search methods created by 'extend' #2274

Open
adfoster-r7 opened this issue Mar 29, 2023 · 1 comment
Open

find-method does not search methods created by 'extend' #2274

adfoster-r7 opened this issue Mar 29, 2023 · 1 comment

Comments

@adfoster-r7
Copy link

Scenario:

Two objects:

  • object_a includes a mixin
  • object_b extends a mixin
module MyMixin
  def mixin_method
    "call to mixin_method #{self}"
  end
end


class ObjectA
  include MyMixin
end

class ObjectB
end


instance_a = ObjectA.new
instance_b = ObjectB.new
instance_b.extend(MyMixin)

require 'pry-byebug'; binding.pry
puts instance_a.method(:mixin_method)
puts instance_b.method(:mixin_method)

The find-method implementation doesn't work with the extend mixin scenario, only the include scenario

Current output:

[16] pry(main)> find-method mixin_method instance_a
MyMixin
MyMixin#mixin_method
[17] pry(main)> find-method mixin_method instance_b
No Methods Matched

Expected output:

[16] pry(main)> find-method mixin_method instance_a
MyMixin
MyMixin#mixin_method
[16] pry(main)> find-method mixin_method instance_a
MyMixin
MyMixin#mixin_method

Ruby itself is able to resolve the methods on the instance as expected:

#<Method: ObjectA(MyMixin)#mixin_method() test.rb:2>
#<Method: ObjectB(MyMixin)#mixin_method() test.rb:2>

Versions

  • pry-0.14.2
@adfoster-r7 adfoster-r7 changed the title find-method does not search include extended methods find-method does not search methods created by 'extend' Mar 29, 2023
@adfoster-r7
Copy link
Author

I think these are the tests that I would like to have passing:

diff --git a/spec/commands/find_method_spec.rb b/spec/commands/find_method_spec.rb
index 7be22752..e489d909 100644
--- a/spec/commands/find_method_spec.rb
+++ b/spec/commands/find_method_spec.rb
@@ -1,6 +1,13 @@
 # frozen_string_literal: true
 
 describe "find-method" do
+
+  MyMixin = Module.new do
+    def mixin_method
+      "linus"
+    end
+  end
+
   MyKlass = Class.new do
     def hello
       "timothy"
@@ -19,6 +26,14 @@ describe "find-method" do
     end
   end
 
+  MyKlassWithMixin = Class.new(MyKlass) do
+    include MyMixin
+  end
+
+  MyInstanceWithMixin = MyKlass.new.tap do |instance|
+    instance.extend(MyMixin)
+  end
+
   describe "find matching methods by name regex (-n option)" do
     it "should find a method by regex" do
       expect(pry_eval(binding, "find-method hell MyKlass")).to match(
@@ -26,6 +41,18 @@ describe "find-method" do
       )
     end
 
+    it "should find included methods by regex" do
+      expect(pry_eval(binding, "find-method mix MyKlassWithMixin")).to match(
+        /MyMixin.*?mixin_method/m
+      )
+    end
+
+    it "should find extended methods by regex" do
+      expect(pry_eval(binding, "find-method mix MyInstanceWithMixin")).to match(
+        /MyMixin.*?mixin_method/m
+      )
+    end
+
     it "should NOT match a method that does not match the regex" do
       expect(pry_eval(binding, "find-method hell MyKlass")).not_to match(
         /MyKlass.*?goodbye/m
@@ -40,6 +67,18 @@ describe "find-method" do
       )
     end
 
+    it "should find included methods by regex" do
+      expect(pry_eval(binding, "find-method -c linus MyKlassWithMixin")).to match(
+        /MyMixin.*?mixin_method/m
+      )
+    end
+
+    it "should find extended methods by regex" do
+      expect(pry_eval(binding, "find-method -c linus MyInstanceWithMixin")).to match(
+        /MyMixin.*?mixin_method/m
+      )
+    end
+
     it "should NOT match a method that does not match the regex" do
       expect(pry_eval(binding, "find-method timothy MyKlass")).not_to match(
         /MyKlass.*?goodbye/m

But currently the MyInstanceWithMixin which use extend(MyMixin) scenarios fail:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant