0Pricing
Ruby Academy · Lesson

Reading and Writing Files

Open, read, write, and modify text files in Ruby.

Reading and Writing Files is a free Ruby Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Reading and Writing Files

Ruby provides powerful methods to interact with files.

In this lesson, you will learn how to open, read, write, and modify text files in Ruby.

Reading and Writing Files — illustration 1

2

Opening and Reading a File

You can open and read a file using File.read.

content = File.read('example.txt')
puts content

3

Reading a File Line by Line

Use File.foreach to read a file line by line.

File.foreach('example.txt') { |line| puts line }

4

Writing to a File

Use File.write to overwrite the contents of a file.

File.write('example.txt', 'Hello, Ruby!')

5

Appending to a File

Use a mode to add content to an existing file.

File.open('example.txt', 'a') { |file| file.puts 'New Line' }

6

Closing a File

Ruby automatically closes files, but you can use file.close if needed.

file = File.open('example.txt', 'r')
puts file.read
file.close

7

Checking if a File Exists

Use File.exist? to check if a file exists before performing operations.

puts File.exist?('example.txt')

8

9

Deleting a File

Use File.delete to remove a file from the system.

File.delete('example.txt') if File.exist?('example.txt')

10

Congratulations!

You have learned how to read, write, and manipulate files in Ruby.

Next, you will explore user input and output handling.

Reading and Writing Files — illustration 10

Frequently asked questions

Is the “Reading and Writing Files” lesson free?

Yes — the full text of “Reading and Writing Files” is free to read here on the web, and the Ruby Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.

What will I learn in “Reading and Writing Files”?

Open, read, write, and modify text files in Ruby. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Reading and Writing Files” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Reading and Writing Files
  2. User Input and Output
  3. Working with JSON and YAML
← Back to Ruby Academy