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

···

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

Hello --

···

On 30/10/2007, Chris Richards <evilgeenius@gmail.com> 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 Adam

Chris Richards 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

···

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

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

···

On Tue, 30 Oct 2007, Chris Richards wrote:

--
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!

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"

···

On Oct 30, 7:11 am, Chris Richards <evilgeen...@gmail.com> 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

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 Richards <evilgeenius@gmail.com> 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 Adam

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

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 30/10/2007, Chris Richards <evilgeenius@gmail.com> 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 Adam

# 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

···

From: Chris Richards [mailto:evilgeenius@gmail.com]