From: Haruka YAGNI Date: 2011-04-01T17:03:08+09:00 Subject: Re: SImple question about file io Hello again, Paul I found another problem. I am sorry not to include this in the previous post. As you set "opf" inside the each_line loop, "opt.puts line"(where I add a mark) won't work when both contents of ifs are not processed, i.e. the opf is not defined inside the loop. To solve this problem, add definition of opf ("opf = nil") before the loop. Look at next examples -- code 1 flag = false 1.upto(3) do |i| unless flag bad_var = 1000 flag = true end puts "#{i}, #{bad_var.nil?}" end -- result 1 1, false 2, true 3, true -- code 2 flag = false bad_var = nil # << bad_var is defined here 1.upto(3) do |i| unless flag bad_var = 1000 flag = true end puts "#{i}, #{bad_var.nil?}" end -- result 2 1, false 2, false 3, false On Fri, Apr 1, 2011 at 4:31 PM, Paul Joyce wrote: > > #puts template > ipf = File.new("All-Contracts-29-Mar-11.csv","r") >      ipf.each_line { |line| >      next if line.match(/^$/) >      data = line.split(",") >      if curr == "x"     #  Initialize >        puts "Initialize . . " + data[0] >        opf = File.new(data[0] + '-feeder-customer.csv',"r+") >        opf.puts colhead   # <-----  HERE >      end >      if curr != data[0] and curr != "x" >         puts "New File . . . " + data[0] >         opf.close >         opf = File.new(data[0] + "-feeder-customer.csv","r+") >         opf.puts colhead >      end >      opf.puts line ## <-- This occurs a nil class error >      curr = data[0] >    } > ipf.close; > opf.close; > > puts 'DONE.' > -- Haruka YAGNI