36

This is what I want to do, but with a one-liner:

lines = Array.new
File.open('test.txt').each { |line| lines << line }

Possible?

4
  • 2
    Be VERY careful reading a file into memory all at once. It's not scalable, and can easily make a program crawl if the file turns out to be bigger than the available memory. Line-by-line is as fast and is the way to go if at all possible. Commented Aug 6, 2014 at 20:42
  • 1
    @theTinMan IO::foreach does file processing line by line. But when someone is trying to get the lines in an array, #readlines very good way to go, rather than ar = []; File.foreach('test.txt') { |line| ar << line }. I didn't check, but I am sure, in C level, readlines() probably does something like that internally already.As opposed to slurping up the whole file at once and then breaking it down into lines. Commented Aug 7, 2014 at 6:04
  • 1
    each is safe because it's the instance version of foreach. I have read the C source, and readlines doesn't scale because it aggregates an entire file into memory. That's a bad practice, even today when machines have more memory and are faster, because files are bigger too. An unexpectedly big file, bigger than Ruby can handle, will take a machine to a crawl. That's unnecessary, and unacceptable in production environments. Commented Aug 7, 2014 at 15:51
  • See "Why is slurping a file bad?" Commented Aug 7, 2014 at 18:30

2 Answers 2

77

Do as below :

File.readlines('test.txt')

Read documentation :

arup@linux-wzza:~> ri IO::readlines

= IO::readlines

(from ruby site)
------------------------------------------------------------------------------
  IO.readlines(name, sep=$/ [, open_args])     -> array
  IO.readlines(name, limit [, open_args])      -> array
  IO.readlines(name, sep, limit [, open_args]) -> array

------------------------------------------------------------------------------

Reads the entire file specified by name as individual lines, and
returns those lines in an array. Lines are separated by sep.

  a = IO.readlines("testfile")
  a[0]   #=> "This is line one\n"

If the last argument is a hash, it's the keyword argument to open. See IO.read
for detail.

Example

arup@linux-wzza:~/Ruby> cat out.txt
name,age,location
Ram,12, UK
Jadu,11, USA
arup@linux-wzza:~/Ruby> ruby -e "p File::readlines('./out.txt')"
["name,age,location\n", "Ram,12, UK\n", "Jadu,11, USA\n"]
arup@linux-wzza:~/Ruby>
Sign up to request clarification or add additional context in comments.

4 Comments

How can't you love Ruby :)
@sandre89 what do you mean? :)
I meant that this is just one more example of such a brilliant and well designed language. You want to "read lines" from a "file" into an array, and Ruby's answer is File.readlines(filename). :)
if your input file has a BOM you may have to specify the encoding such as File.readlines(filename, encoding: 'bom|utf-8')
2

I use the same File.readlines methods as explained in the accepted answer.

However when I read in strings from a text file, I typically want to get rid of the newline characters at the end of each line.

Therefore I use this:

 File.readlines('adopters.txt').map(&:chomp)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.