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

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

Hello –

On 30/10/2007, Chris R. [email protected] wrote:

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 A.

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”}

Hello –

On 30/10/2007, Chris R. [email protected] wrote:

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 A.

Chris R. wrote:

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

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

Hi –

On Tue, 30 Oct 2007, Chris R. wrote:

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? :slight_smile: 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

Hi –

On 30/10/2007, Chris R. [email protected] wrote:

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. :slight_smile:

– Thomas A.

From: Chris R. [mailto:[email protected]]

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"}

have you tried rio?

require “rio”
rio(“/file/location/new.txt”) << astring

kind regards -botp

On Oct 30, 7:11 am, Chris R. [email protected] wrote:

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”