module Byebug::Helpers::FileHelper

Utilities for interaction with files

Public Instance Methods

get_line(filename, lineno) click to toggle source

Reads line number lineno from file named filename

# File lib/byebug/helpers/file.rb, line 18
def get_line(filename, lineno)
  File.open(filename) do |f|
    f.gets until f.lineno == lineno - 1
    f.gets
  end
end
get_lines(filename) click to toggle source

Reads lines of source file filename into an array

# File lib/byebug/helpers/file.rb, line 11
def get_lines(filename)
  File.foreach(filename).reduce([]) { |acc, elem| acc << elem.chomp }
end
n_lines(filename) click to toggle source

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

# File lib/byebug/helpers/file.rb, line 29
def n_lines(filename)
  File.foreach(filename).reduce(0) { |acc, _elem| acc + 1 }
end
normalize(filename) click to toggle source

Regularize file name.

# File lib/byebug/helpers/file.rb, line 36
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) click to toggle source

A short version of a long path

# File lib/byebug/helpers/file.rb, line 47
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) click to toggle source

True for special files like -e, false otherwise

# File lib/byebug/helpers/file.rb, line 57
def virtual_file?(name)
  ["(irb)", "-e", "(byebug)", "(eval)"].include?(name)
end