Skipping the first line when reading in a file in 1.9.3

Andrew Lauer Barinov picture Andrew Lauer Barinov · Mar 8, 2012 · Viewed 14.2k times · Source

I'm using ruby's File to open and read in a text file inside of a rake task. Is there a setting where I can specify that I want the first line of the file skipped? Here's my code so far:

desc "Import users." 
  task :import_users => :environment do 
    File.open("users.txt", "r", '\r').each do |line| 
      id, name, age, email = line.strip.split(',') 
      u = User.new(:id => id, :name => name, :age => age, :email => email) 
      u.save 
    end 
  end

I tried line.lineno and also doing File.open("users.txt", "r", '\r').each do |line, index| and next if index == 0 but have not had any luck.

Answer

michaelmichael picture michaelmichael · Mar 8, 2012

Change each to each_with_index do |line, index| and next if index == 0 will work.