Rails - the number of lines in the file

Hey. How can I get the total number of lines in a file (I don't want to do this with a loop). I am reading a CSV file.

Example 1

CSV.open('clients.csv', 'r')

Example 2

FasterCSV.foreach('clients.csv')

thank.

+3
source share
1 answer

How big is your file?

This option loads the entire file into memory, so if there are problems with size and memory, this may not work.

numrows = FasterCSV.read('clients.csv').size

This parameter uses the built-in Ruby CSV module, which, as you know, is pretty slow, but it works. It also loads the entire file into memory:

numrows = CSV.readlines('clients.csv').size

Both the FasterCSV.read and CSV.readlines files return arrays of arrays, so you can use any array magic you want to get.

+7

Source: https://habr.com/ru/post/1738737/


All Articles