# Newbie question, writing to a file

**URL:** <https://www.ruby-forum.com/t/newbie-question-writing-to-a-file/110311>\
**Category:** Ruby\
**Created:** [July 25, 2007, 7:00am UTC](https://www.ruby-forum.com/t/newbie-question-writing-to-a-file/110311 "2007-07-25T07:00:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jenny\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19adc/32.png) [@Jenny\_P](https://www.ruby-forum.com/u/Jenny_P)\
**Post date:** [July 25, 2007, 7:00am UTC](https://www.ruby-forum.com/t/newbie-question-writing-to-a-file/110311/1 "2007-07-25T07:00:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jenny\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19adc/32.png) [@Jenny\_P](https://www.ruby-forum.com/u/Jenny_P)\
**Post date:** [July 25, 2007, 7:32am UTC](https://www.ruby-forum.com/t/newbie-question-writing-to-a-file/110311/2 "2007-07-25T07:32:26Z")

</div>

Good on you for learning Jenny 🙂

You need to assign your CSV.generate line to a variable, you can call  
it “line” and then, each time you put something in line, you add it to  
the end of an Array that we will call “file\_lines”. Then, once done,  
you can just write the file\_lines out to the file using puts instead  
of write. This is becuase, with f.puts Ruby will put a return at the  
end of each “line” of the “file\_lines” array.

# Something like:

# We have to initialize the array in this case

file\_lines = Array.new

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  
line = 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})”])

# Here is where we put the new line on the end of the array

file\_lines.push(line)

i = i + 1  
end

# The only change here is “puts” not “write”

filename = ‘results.csv’  
File.open filename, ‘w+’ do |f|  
f.puts file\_lines  
end

======================

There are faster ways to do it that I am sure you will get other  
emails from. But this one takes the least amount of modification to  
your own code… try this, get it working, then try some of the more  
advanced examples people will give you.

Regards

Mikel

---

<div class="post-metadata">

**Author:** ![Jenny\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19adc/32.png) [@Jenny\_P](https://www.ruby-forum.com/u/Jenny_P)\
**Post date:** [July 25, 2007, 7:57am UTC](https://www.ruby-forum.com/t/newbie-question-writing-to-a-file/110311/3 "2007-07-25T07:57:52Z")

</div>

On 7/24/07, Jenny P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> 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?

Here’s one way to do it…

File.open(‘results.csv’ , ‘w’) do |file|  
1.upto 5 do |idx|  
file.puts(CSV.generate\_line([“a”, “b”, “c”, idx])  
end  
end

which is probably better than the way I usually do it by throwing the  
String together first…

str\_to\_write = “”  
1.upto 5 do |idx|  
str \<\< CSV.generate\_line([“a”, “b”, “c”, idx]) \<\< “\n”  
end  
File.open(‘results.csv’, ‘w’) { |file| file.write str\_to\_write }

Todd

---

<div class="post-metadata">

**Author:** ![Jenny\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19adc/32.png) [@Jenny\_P](https://www.ruby-forum.com/u/Jenny_P)\
**Post date:** [July 25, 2007, 8:41am UTC](https://www.ruby-forum.com/t/newbie-question-writing-to-a-file/110311/4 "2007-07-25T08:41:28Z")

</div>

On Wed, 25 Jul 2007 14:31:06 +0900, “Mikel L.”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Good on you for learning Jenny 🙂

I am trying, thanks for the encouragement!

> There are faster ways to do it that I am sure you will get other  
> emails from. But this one takes the least amount of modification to  
> your own code… try this, get it working, then try some of the more  
> advanced examples people will give you.

I will do that (try yours to have something working, and then try  
others that come in and see if I can get them working!)

Jenny
