Skip to content

Commit

Permalink
support finding "source" from IRB sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
ccutrer authored Jul 27, 2021
1 parent 836d704 commit e36b99c
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/method_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ def self.comment_helper(source_location, name=nil)
# @return [Array<String>] 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
Expand Down

0 comments on commit e36b99c

Please sign in to comment.