tags:

views:

72

answers:

2

Is there any pre build function by which we can read the whole file without using any loop? So far i have only come across the ones which output line one by one or character one by one.

+3  A: 
IO.read("filename")

or

File.read("filename")
sluukkonen
Docs: http://ruby-doc.org/core/classes/IO.html
quantumSoup
+1  A: 
File.readlines("filename")

This is also a great method to read everything from a file and break split on carriage returns. The return is an Array with one line per element.

Nick
More specifically, `readlines` splits on the internal variable `$/`, which defaults to "\n". You can temporarily reset `$/`, however, and read files into chunks delimited in other ways, too.
Telemachus