From: matt@... (matt neuburg) Date: 2006-11-02T11:15:04+09:00 Subject: Re: Input file, change data, write to file Paul Br wrote: > I'm a ruby newbie trying to read data from a file, make a few changes, > and write the output to a file so it can be imported into a MySQL > database. > > I found a partial solution on page 138 in Maik Schmidt's "Enterprise > Integration with Ruby" book but it lacks a means to write the output to > a file. > > How can I write the output to a file using the below code? I think the most newbie-appealing approach to files is with open() do |f| end because when the block finishes the file closes automatically. The docs on the modes for opening files, and on the methods you need after that, are here: With big data that comes in lines, where each line is to be processed independently, you presumably want two files, reading and writing a line at a time, so the whole operation could be structured like this: def munge(s) return s.gsub(/[aeiou]/, '') # but do your own task here instead end open("path1", "r") do |f1| open("path2", "w") do |f2| f1.each { |line| f2.puts munge(line) } end end m. -- matt neuburg, phd = matt@tidbits.com, http://www.tidbits.com/matt/ Tiger - http://www.takecontrolbooks.com/tiger-customizing.html AppleScript - http://www.amazon.com/gp/product/0596102119 Read TidBITS! It's free and smart. http://www.tidbits.com