In this tutorial, I will show how to read lines from files, with the consideration of performance.
There is a function called
File.readlines in Ruby’s standard library, the usage is:
The document is at: https://ruby-doc.org/core-2.7.0/IO.html#method-c-readlines
readlines will load the entire file into memory firstly, so it maybe performs badly on the large files.
Note 2: By default
readlines still keep the
\n in the lines, if you want to drop the last
\n, you could pass a
chmop: true parameter:
Another function named
IO.foreach will not load the entire file into memory, instead read by chunks.
The document is at: https://ruby-doc.org/core-2.7.0/IO.html#method-c-foreach
Using File.read and then split into lines
Another method is to read the content into one buffer and then split it into lines.
Note different OS use different line format, Unix uses \n, Windows \r\n, MacOs uses \n
Join my Email List for more helpful insights, It's Free!😋