Module: Byebug::Helpers::FileHelper
- Included in:
- BreakCommand, Context, Frame, InfoCommand::FileCommand, Interface, ListCommand
- Defined in:
- lib/byebug/helpers/file.rb
Overview
Utilities for interaction with files
Instance Method Summary collapse
-
#get_line(filename, lineno) ⇒ Object
Reads line number
lineno
from file namedfilename
. -
#get_lines(filename) ⇒ Object
Reads lines of source file
filename
into an array. -
#n_lines(filename) ⇒ Object
Returns the number of lines in file
filename
in a portable, one-line-at-a-time way. -
#normalize(filename) ⇒ Object
Regularize file name.
-
#shortpath(fullpath) ⇒ Object
A short version of a long path.
-
#virtual_file?(name) ⇒ Boolean
True for special files like -e, false otherwise.
Instance Method Details
#get_line(filename, lineno) ⇒ Object
Reads line number lineno
from file named filename
17 18 19 20 21 22 |
# File 'lib/byebug/helpers/file.rb', line 17 def get_line(filename, lineno) File.open(filename) do |f| f.gets until f.lineno == lineno - 1 f.gets end end |
#get_lines(filename) ⇒ Object
Reads lines of source file filename
into an array
10 11 12 |
# File 'lib/byebug/helpers/file.rb', line 10 def get_lines(filename) File.foreach(filename).reduce([]) { |a, e| a << e.chomp } end |
#n_lines(filename) ⇒ Object
Returns the number of lines in file filename
in a portable, one-line-at-a-time way.
28 29 30 |
# File 'lib/byebug/helpers/file.rb', line 28 def n_lines(filename) File.foreach(filename).reduce(0) { |a, _e| a + 1 } end |
#normalize(filename) ⇒ Object
Regularize file name.
35 36 37 38 39 40 41 |
# File 'lib/byebug/helpers/file.rb', line 35 def normalize(filename) return filename if virtual_file?(filename) return File.basename(filename) if Setting[:basename] File.exist?(filename) ? File.realpath(filename) : filename end |
#shortpath(fullpath) ⇒ Object
A short version of a long path
46 47 48 49 50 51 |
# File 'lib/byebug/helpers/file.rb', line 46 def shortpath(fullpath) components = Pathname(fullpath).each_filename.to_a return fullpath if components.size <= 2 File.join('...', components[-3..-1]) end |
#virtual_file?(name) ⇒ Boolean
True for special files like -e, false otherwise
56 57 58 |
# File 'lib/byebug/helpers/file.rb', line 56 def virtual_file?(name) ['(irb)', '-e', '(byebug)', '(eval)'].include?(name) end |