From b944636d00e5b2b7507a491ce4ea10e3b38f517c Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Mon, 26 Jul 2021 18:09:52 -0600 Subject: [PATCH] support finding "source" from IRB sessions --- lib/method_source.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/method_source.rb b/lib/method_source.rb index ffd79ad..dda03d3 100644 --- a/lib/method_source.rb +++ b/lib/method_source.rb @@ -49,6 +49,13 @@ def self.comment_helper(source_location, name=nil) # @return [Array] the contents of the file # @raise [SourceNotFoundError] def self.lines_for(file_name, name=nil) + if file_name == '(irb)' && defined?(IRB) + # we can't memoize, because this "file" is always growing. presumably if you're + # overwriting methods in an IRB session, you're not too concerned about performance + # anyway + return IRB.CurrentContext.io.instance_variable_get(:@line).join.split(/(?<=\n)/) + end + @lines_for_file ||= {} @lines_for_file[file_name] ||= File.readlines(file_name) rescue Errno::ENOENT => e