Module: Byebug::Helpers::FileHelper

Included in:
BreakCommand, Context, Frame, InfoCommand::FileCommand, Interface, ListCommand, SourceFileFormatter
Defined in:
lib/byebug/helpers/file.rb

Overview

Utilities for interaction with files

Instance Method Summary collapse

Instance Method Details

#get_line(filename, lineno) ⇒ Object

Reads line number lineno from file named filename



19
20
21
22
23
24
# File 'lib/byebug/helpers/file.rb', line 19

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



12
13
14
# File 'lib/byebug/helpers/file.rb', line 12

def get_lines(filename)
  File.foreach(filename).reduce([]) { |acc, elem| acc << elem.chomp }
end

#n_lines(filename) ⇒ Object

Returns the number of lines in file filename in a portable, one-line-at-a-time way.



30
31
32
# File 'lib/byebug/helpers/file.rb', line 30

def n_lines(filename)
  File.foreach(filename).reduce(0) { |acc, _elem| acc + 1 }
end

#normalize(filename) ⇒ Object

Regularize file name.



37
38
39
40
41
42
43
# File 'lib/byebug/helpers/file.rb', line 37

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



48
49
50
51
52
53
# File 'lib/byebug/helpers/file.rb', line 48

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

Returns:

  • (Boolean)


58
59
60
# File 'lib/byebug/helpers/file.rb', line 58

def virtual_file?(name)
  ["(irb)", "-e", "(byebug)", "(eval)"].include?(name)
end