Is there a better way to do this? (write something to a file

C

Chris Richards

Is there a better way to write text to a file?

File.open("/file/location/new.txt","w") {|file| file.write "data to
write"}

It just seems a bit long winded and unmessy

Thanks

Chris
 
T

Thomas Adam

Hello --

Is there a better way to write text to a file?

File.open("/file/location/new.txt","w") {|file| file.write "data to
write"}

It just seems a bit long winded and unmessy

File.copy in this instance, surely?

-- Thomas Adam
 
C

Chris Richards

This isn't a file copy. I'm just writing data to a file.

This is equivalent :

File.open("/file/location/new.txt","w") {|file| file << "data to write"}
 
D

Damjan Rems

Chris said:
Is there a better way to write text to a file?

File.open("/file/location/new.txt","w") {|file| file.write "data to
write"}

It just seems a bit long winded and unmessy


You can always write like:

file = File.open("/file/location/new.txt","w")
file.write "data to write"
file.close

But there is no ruby fun there.


by
TheR
 
E

Eustáquio 'TaQ' Rangel

Is there a better way to write text to a file?
You can always write like:

Or you can make this way:

File.open("test.txt","w") {|file| file << "data to write"}

--
Eustáquio "TaQ" Rangel
http://eustaquiorangel.com

"There is no reason for any individual to have a computer in his home."
Ken Olsen
 
T

Thomas Adam

Hi --

This isn't a file copy. I'm just writing data to a file.

This is equivalent :

File.open("/file/location/new.txt","w") {|file| file << "data to write"}

Oops. That's what I get for not drinking coffee before replying. :)

-- Thomas Adam
 
D

David A. Black

Hi --

Is there a better way to write text to a file?

File.open("/file/location/new.txt","w") {|file| file.write "data to
write"}

It just seems a bit long winded and unmessy

Are you sure you mean unmessy? :) In any case -- I agree, it does
have a bit of overhead, and always seems like a lot to write when just
writing one thing. It pays for itself when the block contains more
logic, of course.

You could do:

(File.open("file","w") << data).close

though that makes the writing seem kind of side-effect-esque.


David

--
Upcoming training by David A. Black/Ruby Power and Light, LLC:
* Advancing With Rails, Edison, NJ, November 6-9
* Advancing With Rails, Berlin, Germany, November 19-22
* Intro to Rails, London, UK, December 3-6 (by Skills Matter)
See http://www.rubypal.com for details!
 
B

Brian Adkins

Is there a better way to write text to a file?

File.open("/file/location/new.txt","w") {|file| file.write "data to
write"}

It just seems a bit long winded and unmessy

What would you prefer?

Ruby is quite flexible, so if you start with how you'd like it to
work, you can probably get close. For example (I'm not recommending
these, but if you're looking for brief):

w "/tmp/foo.txt","data to write"

or

"data to write".w "/tmp/foo.txt"

or

"/tmp/foo.txt".w "data to write"
 
P

Peña, Botp

RnJvbTogQ2hyaXMgUmljaGFyZHMgW21haWx0bzpldmlsZ2Vlbml1c0BnbWFpbC5jb21dIA0KIyBU
aGlzIGlzbid0IGEgZmlsZSBjb3B5LiAgSSdtIGp1c3Qgd3JpdGluZyBkYXRhIHRvIGEgZmlsZS4N
CiMgVGhpcyBpcyBlcXVpdmFsZW50IDoNCiMgRmlsZS5vcGVuKCIvZmlsZS9sb2NhdGlvbi9uZXcu
dHh0IiwidyIpIHt8ZmlsZXwgZmlsZSA8PCAiZGF0YSANCiMgdG8gd3JpdGUifQ0KDQpoYXZlIHlv
dSB0cmllZCByaW8/DQoNCnJlcXVpcmUgInJpbyINCnJpbygiL2ZpbGUvbG9jYXRpb24vbmV3LnR4
dCIpIDw8IGFzdHJpbmcNCg0Ka2luZCByZWdhcmRzIC1ib3RwDQo=
 

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
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top