On Jun 12, 1:10 am, barjunk <barj... / attglobal.net> wrote: > On Jun 11, 6:23 pm, "lrleb... / gmail.com" <lrleb... / gmail.com> wrote: > > > > > On Jun 11, 2:42 pm, Li Chen <chen_... / yahoo.com> wrote: > > > > Hi all, > > > > I just wonder what is the Ruby way to update a file in place. Do I need > > > to open a temporary/new file for writing and then rename the temporary > > > file? > > > > Thanks, > > > > Li > > > > -- > > > Posted viahttp://www.ruby-forum.com/. > > > I've used the rio library for this before.http://rio.rubyforge.org/. > > The website has some very good examples on how to use it. > > > Luis > > That's a nice library...I didn't notice any thing about locking the > file object. I'm assuming that that would be done outside the use of > the rio object. > > I was also wondering if you thought you could use that library to read > files that have records that look like: > > start line 1 > line 2 > line 3 > end line4 > > and treat the above four lines as a record? > > Mike B. I used it to clean up some code that was converted from vb6 to vb.net The converter left a bunch of comments in the code that started with "UPGRADE". I also went ahead and change the old "On Error Go To Handler", "ErrHandler", and "Resume Next" strings to vb.net's "Try", "Catch ex as Exception" and "End Try" strings This is what the code looks like require 'rio' rio('C://test').all.files('*.vb') do |f| rio(f) <f.contents.gsub(/'UPGRADE.*\s/,"").gsub(/On Error GoTo ErrHandler/,"Try").gsub(/ErrHandler:/,"Catch ex as Exception\n").gsub(/ Resume Next/,"End Try") end The second line tells rio to look at all the files ending in .vb in the C://test folder The third line does a string substitution in the currently open file BTW, it was very fast. Luis