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. This is lesson 1 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 3 lessons in total.
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.

Opening and Reading a File
You can open and read a file using File.read.
content = File.read('example.txt')
puts contentReading a File Line by Line
Use File.foreach to read a file line by line.
File.foreach('example.txt') { |line| puts line }Writing to a File
Use File.write to overwrite the contents of a file.
File.write('example.txt', 'Hello, Ruby!')Appending to a File
Use a mode to add content to an existing file.
File.open('example.txt', 'a') { |file| file.puts 'New Line' }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.closeChecking if a File Exists
Use File.exist? to check if a file exists before performing operations.
puts File.exist?('example.txt')Deleting a File
Use File.delete to remove a file from the system.
File.delete('example.txt') if File.exist?('example.txt')Congratulations!
You have learned how to read, write, and manipulate files in Ruby.
Next, you will explore user input and output handling.

Learn Ruby with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 29
- Lessons
- 109
Frequently Asked Questions
Is the “Reading and Writing Files” lesson free?
Yes — any 3 lessons of the Ruby Academy learning path, including “Reading and Writing Files”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 3 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.
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
- Reading and Writing Files
- User Input and Output
- Working with JSON and YAML