module Byebug::Helpers::FileHelper
def get_line(filename, lineno)
Reads line number +lineno+ from file named +filename+
def get_line(filename, lineno) File.open(filename) do |f| f.gets until f.lineno == lineno - 1 f.gets end end
def get_lines(filename)
Reads lines of source file +filename+ into an array
def get_lines(filename) File.foreach(filename).reduce([]) { |acc, elem| acc << elem.chomp } end
def n_lines(filename)
one-line-at-a-time way.
Returns the number of lines in file +filename+ in a portable,
def n_lines(filename) File.foreach(filename).reduce(0) { |acc, _elem| acc + 1 } end
def normalize(filename)
Regularize file name.
def normalize(filename) return filename if virtual_file?(filename) return File.basename(filename) if Setting[:basename] File.exist?(filename) ? File.realpath(filename) : filename end
def shortpath(fullpath)
A short version of a long path
def shortpath(fullpath) components = Pathname(fullpath).each_filename.to_a return fullpath if components.size <= 2 File.join("...", components[-3..-1]) end
def virtual_file?(name)
True for special files like -e, false otherwise
def virtual_file?(name) ["(irb)", "-e", "(byebug)", "(eval)"].include?(name) end