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
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
On 10/20/06, Li Chen [email protected] 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
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs