From: Rob Biedenharn Date: 2009-02-01T00:18:37+09:00 Subject: Re: File::write() complement for File::read() ? 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::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. $ 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 Rob@AgileConsultingLLC.com +1 513-295-4739 Skype: rob.biedenharn