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

**URL:** <https://www.ruby-forum.com/t/is-there-a-better-way-to-do-this-write-something-to-a-file/118716>\
**Category:** Ruby\
**Created:** [October 30, 2007, 12:10pm UTC](https://www.ruby-forum.com/t/is-there-a-better-way-to-do-this-write-something-to-a-file/118716 "2007-10-30T12:10:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [October 30, 2007, 12:10pm UTC](https://www.ruby-forum.com/t/is-there-a-better-way-to-do-this-write-something-to-a-file/118716/1 "2007-10-30T12:10:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [October 30, 2007, 12:45pm UTC](https://www.ruby-forum.com/t/is-there-a-better-way-to-do-this-write-something-to-a-file/118716/2 "2007-10-30T12:45:28Z")

</div>

Hello –

On 30/10/2007, Chris R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [October 30, 2007, 1:03pm UTC](https://www.ruby-forum.com/t/is-there-a-better-way-to-do-this-write-something-to-a-file/118716/3 "2007-10-30T13:03:16Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [October 30, 2007, 1:06pm UTC](https://www.ruby-forum.com/t/is-there-a-better-way-to-do-this-write-something-to-a-file/118716/4 "2007-10-30T13:06:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [October 30, 2007, 1:13pm UTC](https://www.ruby-forum.com/t/is-there-a-better-way-to-do-this-write-something-to-a-file/118716/5 "2007-10-30T13:13:38Z")

</div>

> > 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](http://eustaquiorangel.com)

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

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [October 30, 2007, 1:55pm UTC](https://www.ruby-forum.com/t/is-there-a-better-way-to-do-this-write-something-to-a-file/118716/6 "2007-10-30T13:55:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [October 30, 2007, 1:18pm UTC](https://www.ruby-forum.com/t/is-there-a-better-way-to-do-this-write-something-to-a-file/118716/7 "2007-10-30T13:18:19Z")

</div>

Hi –

On 30/10/2007, Chris R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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. 🙂

– Thomas A.

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [October 31, 2007, 2:27am UTC](https://www.ruby-forum.com/t/is-there-a-better-way-to-do-this-write-something-to-a-file/118716/8 "2007-10-31T02:27:59Z")

</div>

From: Chris R. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# 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

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [October 30, 2007, 2:35pm UTC](https://www.ruby-forum.com/t/is-there-a-better-way-to-do-this-write-something-to-a-file/118716/9 "2007-10-30T14:35:59Z")

</div>

On Oct 30, 7:11 am, Chris R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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”
