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?

有帮助吗?

解决方案 2

Use .drop(#rows to ignore):

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

其他提示

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).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top