Ruby 3 · Errors and files

Read a file

Read a whole file, or iterate lines without loading it all.

text = File.read("data.txt")
File.foreach("data.txt") { |line| puts line }

Official Ruby 3 documentation

More errors and files commands

All errors and files commands · Ruby 3 cheatsheet

Was this page helpful?