[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how to print/write the result to a file

Li Chen

10/19/2006 11:19:00 PM

Hi folks,

Assume I have an array=[1,2,3]

How do I print/write the result to a file called test.text either in the
same folder or in a different folder?

Thanks in advance,

Li

--
Posted via http://www.ruby-....

1 Answer

Jano Svitok

10/19/2006 11:31:00 PM

0

On 10/20/06, Li Chen <chen_li3@yahoo.com> wrote:
> Hi folks,
>
> Assume I have an array=[1,2,3]
>
> How do I print/write the result to a file called test.text either in the
> same folder or in a different folder?
>
> Thanks in advance,
>
> Li

It depends on the format you want, e.g.

File.open('path/to/test.txt', 'w') do |f|
f.write(array.join(', '))
end

1. you can use forward slashes (/) even on windows
2. instead of f.write you could use f.puts, f.print, etc.
3. another version is:

f = File.open(',...', 'w')
f.puts ...
f.close

The block version is safer than this as it closes the file
automatically, even in the case of exception within the block.