From: tamouse mailing lists Date: 2013-04-30T04:36:50+09:00 Subject: Re: writing lines to a file --001a11c37b3c7037f604db850166 Content-Type: text/plain; charset=UTF-8 On Apr 29, 2013 11:20 AM, "peteV" wrote: > > I have a text file with on every line a magic card number and such info > > eg: 072 GTC U Mental Vapors > > I'm trying to read the file line for line and append each line to a file named foozled.txt. > I wrote a method to_s it prints to screen right with output : > 072 |GTC| U | Mental Vapors > but I need this output to append this to the foozled file. > > how can this be done? > it now prints nil on each line in the foozled file > > > ------------> snipped code > File.open(cardList,"r") do |file| > file.each_line {|line| line.length > parse=Card.new("#{line}","#{line}","#{line}","#{line}") > > File.open('foozled.txt','a') do |line| line.puts parse.to_s #appends only nil to the file but prints to screen > end > } > end > Just want to say, openning the same output file for every line in the input file seems wasteful. General alg: File.open(outfile,'a') do |cout| File.open(infile,'r') do |cin| cout.puts process(cin.gets) end end --001a11c37b3c7037f604db850166 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable


On Apr 29, 2013 11:20 AM, "peteV" <pete0verse@gmail.com> wrote:
>
> I have a text file with =C2=A0on every line a magic card number and su= ch info
>
> eg: 072 GTC U Mental Vapors
>
> I'm trying to read the file line for line and append each line to = a file named foozled.txt.
> I wrote a method to_s it prints to screen right with output :
> 072 =C2=A0|GTC| U | Mental Vapors
> but I need this output to append this to the foozled file.
>
> how can this be done?
> it now prints nil on each line in the foozled file
>
>
> ------------> snipped code
> File.open(cardList,"r") do |file|
> =C2=A0 =C2=A0 file.each_line {|line| line.length
> =C2=A0 =C2=A0 =C2=A0 =C2=A0parse=3DCard.new("#{line}","= #{line}","#{line}","#{line}")
>
> =C2=A0 =C2=A0 =C2=A0File.open('foozled.txt','a') do |l= ine| line.puts parse.to_s #appends only nil to the file but prints to scree= n
> =C2=A0 =C2=A0 =C2=A0 =C2=A0end
> =C2=A0 =C2=A0 }
> end
>

Just want to say, openning the same output file for every line in the in= put file seems wasteful. General alg:

File.open(outfile,'a') do |cout|
=C2=A0 File.open(infile,'r') do |cin|
=C2=A0=C2=A0=C2=A0 cout.puts process(cin.gets)
=C2=A0 end
end

--001a11c37b3c7037f604db850166--