From: gerry@... (Gerry Wiener) Date: 2001-08-10T08:22:09+09:00 Subject: [ruby-talk:19463] problem with using Tempfile I've written a small script which removes ctrl-M's and an ending ctrl-Z from files which I'm including below. When the lines #ftmp.open() #ftmp.close(true) are uncommented, everything works fine. When they're commented out, I get errors like the following: processing act_func.cpp processing activity.cpp processing anneal.cpp processing confuse.cpp processing conjgrad.cpp processing control.cpp processing direcmin.cpp processing dotprod.cpp processing execute.cpp processing gen_init.cpp /home/wiener/ruby/rmctlm.rb:29:in `process': failed to convert nil into Array (TypeError) from /home/wiener/ruby/rmctlm.rb:24:in `each' from /home/wiener/ruby/rmctlm.rb:24:in `process' from /home/wiener/ruby/rmctlm.rb:15:in `each' from /home/wiener/ruby/rmctlm.rb:15:in `process' from /home/wiener/ruby/rmctlm.rb:59 Does anyone have an idea what's going wrong here? Thanks, Gerry Wiener ------------------------------------------------------------------------------- #!/usr/bin/env ruby require "tempfile" # Remove control M's from file def usage() printf("usage: %s files\n", $0) exit(2) end def process(files) for f in files printf("processing %s\n", f) change = false fp = File.new(f, "r") lines = fp.readlines() fp.close() ftmp = Tempfile.new(File.basename(f)) for l in lines if l[-2] == ?\C-m # Remove control-M outl = l[0...-2] + "\n" ftmp.write(outl) change = true else if l[0] == ?\C-z # Don't write out a line containing control Z change = true else ftmp.write(l) end end end ftmp.close() if change s = sprintf("cp %s %s", ftmp.path(), f) system(s) end #ftmp.open() #ftmp.close(true) end end if (ARGV.length() == 0) usage() end process(ARGV)