Skip to content

Commit 6e88aef

Browse files
authored
Fix show_source command when obj.method is overrided (#1111)
`method` method is sometimes overridden. (example: Net::HTTPRequest) To handle this case, use `Object.instance_method(:method).bind_call` instead of `obj.method(name)`
1 parent d5ee7ca commit 6e88aef

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/irb/source_finder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def method_target(owner_receiver, super_level, method, type)
114114
when "owner"
115115
target_method = owner_receiver.instance_method(method)
116116
when "receiver"
117-
target_method = owner_receiver.method(method)
117+
target_method = Kernel.instance_method(:method).bind_call(owner_receiver, method)
118118
end
119119
super_level.times do |s|
120120
target_method = target_method.super_method if target_method

test/irb/command/test_show_source.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,23 @@ def test_show_source_shows_binary_source
376376
assert_match(%r[Defined in binary file:.+io/console], out)
377377
end
378378

379+
def test_show_source_method_overrided
380+
write_ruby <<~RUBY
381+
class Request
382+
def method; 'POST'; end
383+
def path; '/'; end
384+
end
385+
request = Request.new
386+
binding.irb
387+
RUBY
388+
389+
out = run_ruby_file do
390+
type "show_source request.path"
391+
type "exit"
392+
end
393+
assert_match(%r[#{@ruby_file.to_path}:3\s+def path; '/'; end], out)
394+
end
395+
379396
def test_show_source_with_constant_lookup
380397
write_ruby <<~RUBY
381398
X = 1

0 commit comments

Comments
 (0)