# File::write() complement for File::read()?

**URL:** <https://www.ruby-forum.com/t/file-write-complement-for-file-read/159068>\
**Category:** Ruby\
**Created:** [January 31, 2009, 2:50am UTC](https://www.ruby-forum.com/t/file-write-complement-for-file-read/159068 "2009-01-31T02:50:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [January 31, 2009, 2:50am UTC](https://www.ruby-forum.com/t/file-write-complement-for-file-read/159068/1 "2009-01-31T02:50:10Z")

</div>

Hello,

Why is there not a complementary File::write() method to the  
File::read() method? It feels unbalanced to always write the following  
code whereas it’s so much easier to read a file:

File.open(path, ‘wb’) {|f| f \<\< content }

I would like to see this method in the core Ruby API, just as the  
Symbol#to\_proc() facets method has travelled into the core Ruby API.

Thanks for your consideration.

---

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [January 31, 2009, 9:15am UTC](https://www.ruby-forum.com/t/file-write-complement-for-file-read/159068/2 "2009-01-31T09:15:58Z")

</div>

Allow me to rephrase my question:

It is currently easier to read whole files (via File::read) than to  
write whole files (via File::open and passing in a block). This  
imbalance can be corrected by adding a File::write method, such as the  
following, to the core Ruby API.

def File.write path, data  
File.open(path, ‘wb’) {|f| f \<\< data.to\_s }  
end

Are there any plans to do this? If not, where can I file a request for  
such a change?

Thanks for your consideration.

---

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [January 31, 2009, 4:19pm UTC](https://www.ruby-forum.com/t/file-write-complement-for-file-read/159068/3 "2009-01-31T16:19:03Z")

</div>

On Jan 31, 2009, at 3:16 AM, Suraj K. wrote:

> Are there any plans to do this? If not, where can I file a request  
> for  
> such a change?
> 
> Thanks for your consideration.

## $ fri File#write --------------------------------------------------------------- IO#write ios.write(string) =\> integer

```
  Writes the given string to ios. The stream must be opened for
  writing. If the argument is not a string, it will be converted to
  a string using to_s. Returns the number of bytes written.

     count = $stdout.write( "This is a test\n" )
     puts "That was #{count} bytes of data"

  produces:

     This is a test
     That was 15 bytes of data

```

It’s already there. Do you ever just try these things? This method  
is an instance method.

Your method would be incomplete without a mode. Do you want ‘w’, ‘a’,  
‘wb’, etc.?

def your\_write(path, data, mode=‘wb’)  
File.open(path, mode) {|f| f.write data }  
end

I don’t know why you call #to\_s on data if you open ‘wb’. (IO#write  
does that for you if what you pass isn’t a String.)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid  
+1 513-295-4739  
Skype: rob.biedenharn

---

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [January 31, 2009, 6:53pm UTC](https://www.ruby-forum.com/t/file-write-complement-for-file-read/159068/4 "2009-01-31T18:53:18Z")

</div>

Rob B. wrote:

> $ fri File#write  
> […]  
> It’s already there. This method is an instance method.

I asked for a class method File::write (just like File::read) not an  
instance method File#write.

> Your method would be incomplete without a mode. Do you want ‘w’, ‘a’,  
> ‘wb’, etc.?
> 
> def your\_write(path, data, mode=‘wb’)  
> File.open(path, mode) {|f| f.write data }  
> end

Good point.

> I don’t know why you call #to\_s on data if you open ‘wb’. (IO#write  
> does that for you if what you pass isn’t a String.)

Thanks for the tip.

---

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [January 31, 2009, 9:48pm UTC](https://www.ruby-forum.com/t/file-write-complement-for-file-read/159068/5 "2009-01-31T21:48:02Z")

</div>

Suraj K. wrote:

> where can I file a request for such a change?

I filed this feature request on Ruby Issue Tracking System:

[http://redmine.ruby-lang.org/issues/show/1081](http://redmine.ruby-lang.org/issues/show/1081)
