Bird
Raised Fist0
Rubyprogramming~5 mins

IO modes (r, w, a) in Ruby

Choose your learning style10 modes available
Introduction

We use IO modes to tell the computer how we want to open a file: to read, write, or add more content.

When you want to read information from a file, like reading a list of names.
When you want to create a new file or replace everything in an existing file with new content.
When you want to add new information to the end of an existing file without deleting what is already there.
Syntax
Ruby
File.open('filename', 'mode') do |file|
  # work with file here
end

'r' means open the file for reading only.

'w' means open the file for writing, and it will erase the file if it exists or create a new one.

'a' means open the file for appending, so new content is added at the end.

Examples
This reads and prints the whole content of 'example.txt'.
Ruby
File.open('example.txt', 'r') do |file|
  content = file.read
  puts content
end
This writes 'Hello, world!' to 'example.txt', erasing old content.
Ruby
File.open('example.txt', 'w') do |file|
  file.puts 'Hello, world!'
end
This adds a new line at the end of 'example.txt' without removing old content.
Ruby
File.open('example.txt', 'a') do |file|
  file.puts 'Adding a new line.'
end
Sample Program

This program first writes two lines to a file, then adds a third line, and finally reads and prints all lines.

Ruby
filename = 'testfile.txt'

# Write mode: create or overwrite file
File.open(filename, 'w') do |file|
  file.puts 'Line 1'
  file.puts 'Line 2'
end

# Append mode: add more lines
File.open(filename, 'a') do |file|
  file.puts 'Line 3'
end

# Read mode: read and print all lines
File.open(filename, 'r') do |file|
  file.each_line do |line|
    puts line.chomp
  end
end
OutputSuccess
Important Notes

Using 'w' mode deletes old file content, so be careful if you want to keep it.

Using 'a' mode keeps old content and adds new content at the end.

Always close files or use blocks (do...end) to close automatically.

Summary

'r' mode is for reading files without changing them.

'w' mode is for writing files and erases old content.

'a' mode is for adding new content to the end of files.

Practice

(1/5)
1. What does the 'r' mode do when opening a file in Ruby?
easy
A. Opens the file for reading only, without changing its content.
B. Opens the file for writing and erases existing content.
C. Opens the file for appending new content at the end.
D. Creates a new file if it doesn't exist and writes to it.

Solution

  1. Step 1: Understand the purpose of 'r' mode

    The 'r' mode is used to open a file only for reading, so the file content is not changed.
  2. Step 2: Compare with other modes

    'w' mode writes and erases content, 'a' appends, but 'r' only reads.
  3. Final Answer:

    Opens the file for reading only, without changing its content. -> Option A
  4. Quick Check:

    'r' = read only [OK]
Hint: Remember 'r' stands for read only, no changes made [OK]
Common Mistakes:
  • Confusing 'r' with 'w' and thinking it erases content
  • Assuming 'r' can write to the file
  • Mixing 'r' with 'a' mode functionality
2. Which of the following is the correct syntax to open a file named data.txt for appending in Ruby?
easy
A. File.open('data.txt', 'x')
B. File.open('data.txt', 'w')
C. File.open('data.txt', 'r')
D. File.open('data.txt', 'a')

Solution

  1. Step 1: Identify the mode for appending

    The mode 'a' is used to open a file for appending new content at the end.
  2. Step 2: Check the syntax

    The correct syntax is File.open('filename', 'a') to append.
  3. Final Answer:

    File.open('data.txt', 'a') -> Option D
  4. Quick Check:

    Append mode = 'a' [OK]
Hint: Use 'a' to add content without erasing [OK]
Common Mistakes:
  • Using 'r' which is read-only mode
  • Using 'w' which erases file content
  • Using 'x' which creates a new file and errors if exists
3. What will be the output of this Ruby code if example.txt initially contains "Hello"?
File.open('example.txt', 'w') { |f| f.puts 'World' }
content = File.read('example.txt')
puts content
medium
A. Hello\nWorld
B. Hello
C. World
D. Error: file not found

Solution

  1. Step 1: Understand 'w' mode effect

    Opening with 'w' erases existing content and writes new content.
  2. Step 2: Analyze the code output

    The file is overwritten with "World" and then read back, so output is "World" with a newline.
  3. Final Answer:

    World -> Option C
  4. Quick Check:

    'w' erases old content, so output is new content with newline [OK]
Hint: Writing with 'w' erases old content, so only new text shows [OK]
Common Mistakes:
  • Thinking 'w' appends instead of overwriting
  • Expecting old content to remain
  • Confusing output with combined old and new text
4. The following Ruby code is intended to add the line "Goodbye" to the end of log.txt. What is the error?
File.open('log.txt', 'w') do |file|
  file.puts 'Goodbye'
end
medium
A. The file name should be in double quotes, not single quotes.
B. Using 'w' mode erases the file instead of appending.
C. Missing a block parameter for the file object.
D. The method 'puts' cannot be used inside File.open.

Solution

  1. Step 1: Identify the mode used

    The code uses 'w' mode which overwrites the file content.
  2. Step 2: Understand the intended action

    To add content without erasing, 'a' mode should be used instead of 'w'.
  3. Final Answer:

    Using 'w' mode erases the file instead of appending. -> Option B
  4. Quick Check:

    Append needs 'a', not 'w' [OK]
Hint: Use 'a' to add lines, 'w' erases file [OK]
Common Mistakes:
  • Confusing 'w' with append mode
  • Thinking 'w' adds content without erasing
  • Believing quotes style affects file opening
5. You want to create a Ruby script that reads a file notes.txt, appends the line "New note added", and then reads the updated content to print it. Which sequence of file modes should you use?
hard
A. Open with 'r' to read, then 'a' to append, then 'r' to read again.
B. Open with 'a' to append, then 'r' to read the updated content.
C. Open with 'w' to write, then 'a' to append, then 'r' to read.
D. Open with 'r' to read, then 'w' to write, then 'r' to read again.

Solution

  1. Step 1: Read the original file

    Use 'r' mode to read the existing content without changing it.
  2. Step 2: Append the new line

    Use 'a' mode to add "New note added" at the end without erasing.
  3. Step 3: Read the updated content

    Open again with 'r' mode to read and print the updated file content.
  4. Final Answer:

    Open with 'r' to read, then 'a' to append, then 'r' to read again. -> Option A
  5. Quick Check:

    Read, append, read = 'r', 'a', 'r' [OK]
Hint: Read first, append with 'a', then read again [OK]
Common Mistakes:
  • Using 'w' which erases content before appending
  • Trying to append before reading original content
  • Not reopening file to read updated content
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard