diff --git a/lib/pry/wrapped_module.rb b/lib/pry/wrapped_module.rb index 3a508de0..35c5931d 100644 --- a/lib/pry/wrapped_module.rb +++ b/lib/pry/wrapped_module.rb @@ -206,13 +206,14 @@ class Pry return nil if !file.is_a?(String) - class_regex1 = /#{mod_type_string}\s*(\w*)(::)?#{wrapped.name.split(/::/).last}/ - class_regex2 = /(::)?#{wrapped.name.split(/::/).last}\s*?=\s*?#{wrapped.class}/ + class_regexes = [/#{mod_type_string}\s*(\w*)(::)?#{wrapped.name.split(/::/).last}/, + /(::)?#{wrapped.name.split(/::/).last}\s*?=\s*?#{wrapped.class}/, + /(::)?#{wrapped.name.split(/::/).last}\.(class|instance)_eval/] host_file_lines = lines_for_file(file) search_lines = host_file_lines[0..(line - 2)] - idx = search_lines.rindex { |v| class_regex1 =~ v || class_regex2 =~ v } + idx = search_lines.rindex { |v| class_regexes.any? { |r| r =~ v } } [file, idx + 1] rescue Pry::RescuableException diff --git a/test/helper.rb b/test/helper.rb index 2e3ac4c0..144e01a3 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -46,6 +46,17 @@ class TestClassForShowSource end end +class TestClassForShowSourceClassEval + def alpha + end +end + +class TestClassForShowSourceInstanceEval + def alpha + end +end + + # in case the tests call reset_defaults, ensure we reset them to # amended (test friendly) values class << Pry diff --git a/test/test_default_commands/test_show_source.rb b/test/test_default_commands/test_show_source.rb index fe46fab7..50eda74f 100644 --- a/test/test_default_commands/test_show_source.rb +++ b/test/test_default_commands/test_show_source.rb @@ -289,6 +289,9 @@ if !mri18_and_no_real_source_location? end end + # note that pry assumes a class is only monkey-patched at most + # ONCE per file, so will not find multiple monkeypatches in the + # SAME file. describe "show-source -a" do it 'should show the source for all monkeypatches defined in different files' do class TestClassForShowSource @@ -300,6 +303,26 @@ if !mri18_and_no_real_source_location? result.should =~ /def alpha/ result.should =~ /def beta/ end + + it 'should show the source for a class_eval-based monkeypatch' do + TestClassForShowSourceClassEval.class_eval do + def class_eval_method + end + end + + result = mock_pry("show-source TestClassForShowSourceClassEval -a") + result.should =~ /def class_eval_method/ + end + + it 'should show the source for an instance_eval-based monkeypatch' do + TestClassForShowSourceInstanceEval.instance_eval do + def instance_eval_method + end + end + + result = mock_pry("show-source TestClassForShowSourceInstanceEval -a") + result.should =~ /def instance_eval_method/ + end end end end