Files and IO

Read a whole file

The method File.read loads a whole file into one string, which suits small configs and study scripts. Reading huge logs this way can exhaust memory, so treat it as the small file tool. The script below reads its own source file, so it runs in any folder.

text = File.read(__FILE__) puts text.empty?

ruby read_self.rb

false

Iterate lines

The iterator File.foreach yields each line in turn while keeping memory flat. Every line keeps its trailing newline, and chomp trims it away. Prefer this form for logs and dumps of unknown size.

File.write("demo.txt", "a\nb\n") File.foreach("demo.txt") do |line| puts line.chomp end

ruby lines_demo.rb

a
b

Write files

The method File.write replaces the whole file content with the given string. Opening with mode w in a block serves several writes in one session. Newlines never appear on their own: every line break comes from the strings you pass.

File.write("demo.txt", "hello") puts File.read("demo.txt")

ruby write_demo.rb

hello

List a directory

The method Dir.children lists the names inside a folder. The predicate Dir.exist? checks that a folder is present before touching it. The method File.join assembles folder and file names into a correct path on any system.

names = Dir.children(".") puts names.is_a?(Array) puts Dir.exist?(".")

ruby dir_demo.rb

true
true

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does File.read(__FILE__) read?

    Answer

    The whole source file of the running script, as one string

    __FILE__ is the path of the script, and File.read loads the whole file into one string.

  2. What does this program print? File.write("demo.txt", "a\nb\n") File.foreach("demo.txt") do |line| puts line.chomp end

    Answer

    a / b

    foreach yields one line at a time and chomp removes the newline that each line carries.

  3. What does this program print? File.write("demo.txt", "hello") puts File.read("demo.txt")

    Answer

    hello

    File.write replaces the file with the string, and puts prints it without quotes.

  4. What does this program print? File.write("demo.txt", "a\nb\n") p File.read("demo.txt")

    Answer

    "a\nb\n"

    File.read keeps the newlines, and p shows the string with its escapes and quotes.

  5. What does this program print? p Dir.children(".").class

    Answer

    Array

    Dir.children answers the names as an array of strings.

  6. What does this program print? p Dir.exist?("no_such_folder")

    Answer

    false

    Dir.exist? answers false for a missing folder instead of raising.

  7. Why does File.foreach suit a huge log better than File.read?

    Answer

    It yields one line at a time and keeps memory flat

    File.read holds the whole file in memory at once.

  8. What does line.chomp do?

    Answer

    Removes the trailing newline from the line

    Every line of File.foreach keeps its trailing newline, and chomp trims exactly that.

  9. What does opening a file with mode w do to its content?

    Answer

    Replaces the whole content

    Mode w truncates the file; mode a appends.

  10. What does this program print? p File.join("logs", "app.txt")

    Answer

    "logs/app.txt"

    File.join builds the path with the separator of the system, and p prints the string with its quotes.

Article author: Arthur Isaev