Method: String#read_file

Defined in:
lib/na/string.rb

#read_fileString

Returns the contents of the file, or raises if missing. Handles directories and NA extension.

Raises:

  • (RuntimeError)

    if the file does not exist



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/na/string.rb', line 56

def read_file
  file = File.expand_path(self)
  raise "Missing file #{file}" unless File.exist?(file)

  if File.directory?(file)
    if File.exist?("#{file}.#{NA.extension}")
      file = "#{file}.#{NA.extension}"
    elsif File.exist?("#{file}/#{File.basename(file)}.#{NA.extension}")
      file = "#{file}/#{File.basename(file)}.#{NA.extension}"
    else
      NA.notify("#{NA.theme[:error]}#{file} is a directory", exit_code: 2)
    end
  end

  # IO.read(file).force_encoding('ASCII-8BIT').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?')
  File.read(file).force_encoding('utf-8')
end