Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > File::write() complement for File::read() ?

Reply
Thread Tools

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

 
 
Suraj Kurapati
Guest
Posts: n/a
 
      01-31-2009
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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Suraj Kurapati
Guest
Posts: n/a
 
      01-31-2009
Allow me to rephrase my question:

It is currently easier to read whole files (via File::read) than to
write whole files (via File:pen 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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Rob Biedenharn
Guest
Posts: n/a
 
      01-31-2009

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

> Allow me to rephrase my question:
>
> It is currently easier to read whole files (via File::read) than to
> write whole files (via File:pen 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.


$ 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 Biedenharn http://agileconsultingllc.com
http://www.velocityreviews.com/forums/(E-Mail Removed)
+1 513-295-4739
Skype: rob.biedenharn



 
Reply With Quote
 
Suraj Kurapati
Guest
Posts: n/a
 
      01-31-2009
Rob Biedenharn 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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Suraj Kurapati
Guest
Posts: n/a
 
      01-31-2009
Suraj Kurapati 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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
2's complement vs. 1's complement vs. ... Roberto Waltman C Programming 4 06-13-2011 11:26 PM
1's complement and 2's complement sarathy C++ 22 08-02-2006 05:53 PM
1's complement and 2's complement sarathy C Programming 20 08-02-2006 05:53 PM
sign magnitude, ones complement, two's complement Mantorok Redgormor C Programming 8 10-07-2003 11:52 PM
Conversion 1QN -> 2'Complement Troels Smit VHDL 1 07-01-2003 09:43 AM



Advertisments