-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Zoe Phoenix wrote:
| Can anybody tell me how to take the output of a method and save it to a
| file?
|
| I have an array and I've created a method to take the strings I put into
| the array and insert them into a place in a sentence. How do I execute
| the method and save the output to a file?  I just started with Ruby a
| few days ago and I can have it save a file, but the file it saves is
| empty.
|
| Help, please?

Sure:
irb(main):003:0> to_file = [1,2,3,4,5]
=> [1, 2, 3, 4, 5]
irb(main):004:0> File.open("a_file.txt","w") do |f|
irb(main):005:1* f.write to_file
irb(main):006:1> end
=> 5
irb(main):007:0> exit

C:\temp>cat a_file.txt
12345

Let's walk a bit through the code:
to_file is your average Array.

The File class allows you to interact with files (most commonly, reading
and writing).

Fil.open("a_file.txt","w") opens a file called "a_file.txt" in write
mode (the "w"). It's important to tell Ruby the mode you want to use.
"r" is the mode to read files, "a" is the mode to append to a file, and
Windows has the mode "b" to handle binary files.
You can find a quick reference for that here:
http://www.zenspider.com/Languages/Ruby/QuickRef.html#14

The |f| is a bloack variable. I use that in the next line, to actually
write to the file:
f.write to_file This writes the content of the Array to a file. Note
that the Array is dumped similar to Array#join "". You'll have to add
line breaks somehow, if you want lines.

Anyway: This step in the block writes to the file.

end closes the block, and with that, Ruby takes care of closing the
file, too, so that you don't have to.

I hope that helps. (I have to look it up myself, more often than I would
like).

- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan

~   YAAH! DEATH TO OATMEAL!
		  -- Calvin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkf8BVcACgkQbtAgaoJTgL8HJACgjQcC3qMkIpm+VXUN0E/PdmUy
/dwAn3q5ECZNyH8bGwk9brT9sJmSZoOx
=z49y
-----END PGP SIGNATURE-----