Writing to a file

A

Alexander McMillan

Take a look at http://www.oldkingjames.org it has Ruby lessons for beginner=
s in Learn to program and there are a couple of pages that deal with the Fi=
le modes r r+ w w+ a a+ b used to write to files.
=20
Date: Tue=2C 3 May 2011 02:37:40 +0900
From: (e-mail address removed)
Subject: Writing to a file
To: (e-mail address removed)
=20
Hey guys=2C can anyone explain how to write to a file in Ruby?
=20
Thanks=2C
=20
Mike
=20
--=20
Posted via http://www.ruby-forum.com/.
=20
=
 
V

Vicente Bosch Campos

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=20

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

Regards,
Vicente
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,109
Messages
2,570,671
Members
47,263
Latest member
SyreetaGru

Latest Threads

Top