Reading a File Line-by-Line with File.readlines in Ruby
📖 Scenario: You have a text file named colors.txt that contains a list of colors, one color per line. You want to read this file line-by-line to process each color.
🎯 Goal: Build a Ruby program that reads the colors.txt file line-by-line using File.readlines and prints each color in uppercase.
📋 What You'll Learn
Create a variable called
colors that reads all lines from colors.txt using File.readlinesCreate a variable called
count and set it to 0Use a
for loop with variable color to iterate over colorsInside the loop, print each
color in uppercaseAfter the loop, print the total number of colors read
💡 Why This Matters
🌍 Real World
Reading files line-by-line is common when processing logs, configuration files, or data lists.
💼 Career
Many programming jobs require reading and processing text files efficiently, especially in scripting and automation.
Progress0 / 4 steps