From: Rob Biedenharn Date: 2008-04-24T21:46:34+09:00 Subject: Re: FasterCSV.foreach lineno? On Apr 24, 2008, at 7:55 AM, Bil Kleb wrote: > OK, so I can't think my way out of a paper this morning. > > Can I access FasterCVS#lineno while inside a foreach? > > For example, > > $ cat << EOF > csv_file > one > two > three > EOF > > $ ruby -rubygems \ > -e'require "fastercsv";FasterCSV.foreach("csv_file")\ > {|r| puts "lineno somehow"}' > > Thanks, > -- > Bil Kleb > http://fun3d.larc.nasa.gov Bil, Since it looks like the answer to your first question is "No.", what if you defined: class FasterCSV def self.foreach_with_lineno(path, options = Hash.new, &block) open(path, options) do |csv| csv.each {|line| block[line, csv.lineno]} end end end irb> FasterCSV.foreach_with_lineno("csv_file") {|r,l| puts "#{l}: #{r}"} 1: one 2: two 3: three => nil -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com