Ruby
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
35: Building a Simple File-Based Task Tracker
When you first start building apps that actually save data, you'll likely run into a frustrating wall the moment you try to load your data. I remember doing this early on: I'd spend an hour writing a beautiful logic system, hit "run," and immediately get a crash before the program even started. Here is a piece of code that looks perfectly reasonable, but it's a trap.
def load_tasks
File.readlines("tasks.txt").map { |line| line.chomp }
end
tasks = load_tasks
puts "Your tasks: #{tasks}"
The 'Missing File' Crash
If you run that code on a fresh machine, Ruby will throw an Errno::ENOENT (No such file or directory @ rb_sysopen - tasks.txt). The problem is that File.readlines assumes the file already exists. It doesn't say, "Oh, I don't see a file here, I'll just return an empty list." It just panics and dies.
Handling the Initial Run
To fix this, we need to be defensive. We should check if the file exists before we try to read it. If it doesn't, we simply return an empty array. This allows the program to boot up normally the first time a user runs it, and then it will work as expected once the program creates the file for the first time.
def load_tasks
return [] unless File.exist?("tasks.txt")
File.readlines("tasks.txt").map { |line| line.chomp }
end
Now, let's build this out into a proper tracker. We want something that doesn't just read and write, but manages the state of our tasks during a session.
Structuring the Task Logic
I find it's much cleaner to wrap this in a class. It keeps our file-handling logic separate from the user interface. We'll use a simple array of strings for this version, but in a real-world app, you'd likely use an array of objects or a CSV.
class TaskTracker
FILE_NAME = "tasks.txt"
def initialize
@tasks = load_tasks
end
def add(task_text)
@tasks << task_text
save_tasks
end
def list
@tasks.each_with_index { |task, i| puts "#{i + 1}. #{task}" }
end
def remove(index)
@tasks.delete_at(index - 1)
save_tasks
end
private
def load_tasks
return [] unless File.exist?(FILE_NAME)
File.readlines(FILE_NAME).map { |line| line.chomp }
end
def save_tasks
File.open(FILE_NAME, "w") do |file|
file.puts @tasks
end
end
end
Persisting Tasks to Disk
Notice the save_tasks method. I used File.open(FILE_NAME, "w"). The "w" flag is crucial here—it tells Ruby to truncate the file (wipe it clean) before writing. If we used "a" (append), every time we saved our list, we'd just keep adding the entire list to the end of the file, creating a massive, redundant mess.
The block syntax do |file| ... end is the professional way to handle files. It ensures that the file is closed automatically even if an error occurs inside the block. Manually calling file.close is a habit you should break early; it's too easy to forget, and leaking file descriptors is a great way to crash a production server.
To tie it all together, you'd just need a simple loop in your main script to call these methods based on user input. You've now moved from "volatile" memory (where data disappears when the program stops) to "persistent" storage.
📋 Practical Task
Implementing a Task Completion Toggle
Modify the TaskTracker class provided in the lesson to support "completed" tasks. Since we are using a simple text file, you can't just store a boolean. You'll need to change how tasks are stored in the file.
- The Requirement: Tasks should be stored in the format
"[ ] Task name"for incomplete tasks and"[x] Task name"for completed ones. - The Method: Add a method called
toggle_task(index)that finds the task at the given index and switches its status between[ ]and[x]. - The Update: Update the
addmethod so that every new task starts with the"[ ] "prefix. - The Logic: Ensure that when
save_tasksis called, the updated prefixes are written correctly to the file.
There are no comments for now.