J
Jenny Purcell
I'm reading Learn To Program, which is based around Ruby code, and it
has directions on how to write to a named file.
It looks like following these directions, I want to do something like
this, where "string" is the text I want to write to the file.
filename = 'results.csv'
File.open filename, 'w' do |f|
f.write string
Howver, what I can't sort out is how exactly I get the string to write
to the file.
Here's the code I use now:
# Print out results to csv file line by line
while i <= count do
j = number.detect {|c| position[c - 1] == i + 1}
j = j - 1
# Push each horse's name, owner, score, pre_points, dice rolls,
and other info into a new csv file
puts CSV.generate_line(["pp#{pole_new[j]}", horse_name[j],
horse_info[j], owners_initials[j], best_result[j], race_style[j],
best_time[j], pre_points[j], roll_1[j], roll_2[j], roll_3[j],
roll_4[j], roll_5[j], score[j], "#{position[j]}(#{count + 1})"])
i = i + 1
end
Then, what I do is run the program and use 'racing.rb > results.csv'
to actually write to a results file. I'd like the next step to be that
it writes to its own results file without me having to name it each
time I run the program.
How do I set up the CSV lines that I generate to be put in a string
(or object) that I can then send to a file?
Jenny
has directions on how to write to a named file.
It looks like following these directions, I want to do something like
this, where "string" is the text I want to write to the file.
filename = 'results.csv'
File.open filename, 'w' do |f|
f.write string
Howver, what I can't sort out is how exactly I get the string to write
to the file.
Here's the code I use now:
# Print out results to csv file line by line
while i <= count do
j = number.detect {|c| position[c - 1] == i + 1}
j = j - 1
# Push each horse's name, owner, score, pre_points, dice rolls,
and other info into a new csv file
puts CSV.generate_line(["pp#{pole_new[j]}", horse_name[j],
horse_info[j], owners_initials[j], best_result[j], race_style[j],
best_time[j], pre_points[j], roll_1[j], roll_2[j], roll_3[j],
roll_4[j], roll_5[j], score[j], "#{position[j]}(#{count + 1})"])
i = i + 1
end
Then, what I do is run the program and use 'racing.rb > results.csv'
to actually write to a results file. I'd like the next step to be that
it writes to its own results file without me having to name it each
time I run the program.
How do I set up the CSV lines that I generate to be put in a string
(or object) that I can then send to a file?
Jenny