From: "Brian Schröder" Date: 2005-10-03T22:49:23+09:00 Subject: Re: ruby gsub! problem On 03/10/05, Kev Jackson wrote: > > [snip] > > Here's what I ended up with - love blocks, without them there'd be a lot > more resource handling... > > File.open("sections.sql", "w") do |output| > File.open("TBLSECTION.dat","r") do |input| > lines=0 > # here I was trying to remove the \n > input.to_s.gsub!(/\n/, '') Here your create a string from the input and substitute the newlines against the empty string. You then forget about the string and it gets garbage collected. Try content = input.read content.gsub!(/\n/, '') and replace the below against content.gsub!(/;/, ";\n") and then write the content to a file output << content > # didn't work so moved into munge_fields > input.each(";") do |line| > munge_fields line > munge_data(line, lines+1) > p line > output << line << "\n" > lines+=1 > end > p "Total Lines in file : " + lines.to_s > end > end > > both munge_fields and munge_data do the sub! thing to bring the data > back to the format I want, and although I considered splitting to get an > array of strings then creating a builder to rebuild my output, for now > it works fine with simple regexps > > Kev > Or you do it in a more concise way: File.open("sections.sql", "w") do |output| output << File.read("TBLSECTION.dat").gsub(/\n/, '').gsub(/;/, ";\n") end hope to help, Brian -- http://ruby.brian-schroeder.de/ Stringed instrument chords: http://chordlist.brian-schroeder.de/