Zest.

Ruby: Read a file

A short, tested Ruby example for read a file. Copy it and adjust the names and paths for your project.

Ruby
text = File.read("data.txt")
File.foreach("data.txt") { |line| puts line }
File.write("out.txt", "hello\n")

Find it in Zest

Search for “ruby read a file” in Zest and this example appears above the results, with other matches to choose from. Searches stay private.

More Ruby examples

All Ruby examples