Question

I have a rake file that pulls in data from an external CSV file and enumerates through it with:

CSV.foreach(file, :headers => true) do |row|

What is the most effective way (in ruby) to specify the starting point within the spreadsheet?

:headers => true allows me to start importing from the second line, but what if I want to start a line 20?

Was it helpful?

Solution 2

Use .drop(#rows to ignore):

CSV.open(file, :headers => true).drop(20).each do |row|

OTHER TIPS

Ruby enumerators include a drop method that will skip over the first n items.

When not passed a block, CSV.foreach returns an enumerator.

You can use

CSV.foreach(file, :headers => true).drop(20).each do |row|

This will skip the first 20 data rows (the header row does NOT count as one of those twenty).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top