Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > how to print/write the result to a file

Reply
Thread Tools

how to print/write the result to a file

 
 
Li Chen
Guest
Posts: n/a
 
      10-19-2006
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-forum.com/.

 
Reply With Quote
 
 
 
 
Jan Svitok
Guest
Posts: n/a
 
      10-19-2006
On 10/20/06, Li Chen <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
i = 10; result = ++i - --i; How result become ZERO Lakshmi Sreekanth C Programming 52 09-23-2010 07:41 AM
Is the result of valid dynamic cast always equal to the result ofcorrespondent static cast? Pavel C++ 7 09-18-2010 11:35 PM
simulation result is correct but synthesis result is not correct J.Ram VHDL 7 12-03-2008 01:26 PM
1. Ruby result: 101 seconds , 2. Java result:9.8 seconds, 3. Perl result:62 seconds Michael Tan Ruby 32 07-21-2005 03:23 PM



Advertisments