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
File.foreach(cardList) do|line|
parse= Card.new( *Array.new( 4, line.to_s ) )
File.open(‘foozled.txt’,‘a’) {|f| f.puts parse.to_s }
end
no, it gives the same output nil’s to the file, prints good to screen
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.
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
well, ok, that would have meant opening, appending a line and closing it
17,000 times in the original. it is sometimes hard to conceive of
computers
doing actual work, but that is a lot of unnecessary opens and closes.
otoh, 17,000 lines may not costbthat much ram if read in all at one
time,
filtered to a buffer, and appended en masse. so joel does have a point.
a billion lines is more into the problem space of too much to buffer.
(or
in the case i am familiar with, 4GB database file)
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.