Writing to a file

Hey guys, can anyone explain how to write to a file in Ruby?

Thanks,

Mike

···

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

Have you heard about google ?

http://snippets.dzone.com/posts/show/5051
http://www.ruby-doc.org/core/classes/File.html
http://forums.site5.com/showthread.php?t=8163

···

----
http://blog.eugen.co

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

Take a look at http://www.oldkingjames.org it has Ruby lessons for beginners in Learn to program and there are a couple of pages that deal with the File modes r r+ w w+ a a+ b used to write to files.

···

Date: Tue, 3 May 2011 02:37:40 +0900
From: mlc0613@yahoo.com
Subject: Writing to a file
To: ruby-talk@ruby-lang.org

Hey guys, can anyone explain how to write to a file in Ruby?

Thanks,

Mike

--
Posted via http://www.ruby-forum.com/\.

Easiest way is to open a file for writing and with a block write the contents to it.

File.open("test_file.txt", "a+") do |file| #opens the file "test_file.txt" in append mode, if it does not exist it creates it.
    file.puts "Hello world"
end #closes the file for you

Otherway is to use File.new but that forces you to ensure to close the file etc.

Regards,
Vicente

···

On May 2, 2011, at 7:37 PM, Mike Jones wrote:

Hey guys, can anyone explain how to write to a file in Ruby?

Thanks,

Mike

--
Posted via http://www.ruby-forum.com/\.