Ruby way to update a file in place

L

Li Chen

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
 
D

Daniel Berger

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?

That's the usual way. The other way is to use mmap.

Regards,

Dan
 
J

Joel VanderWerf

Li said:
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?

You can open with the "r+" file mode, and use seek/rewind to move to a
position within the file, use #truncate to end the file, and so on.

[~/tmp] cat >foo
foo
bar
baz
[~/tmp] irb
irb(main):001:0> f = File.open("foo", "r+")
=> #<File:foo>
irb(main):002:0> f.puts "FOO"
=> nil
irb(main):003:0> f.seek 8
=> 0
irb(main):004:0> f.puts "BAZ"
=> nil
irb(main):005:0> f.rewind
=> 0
irb(main):006:0> f.read
=> "FOO\nbar\nBAZ\n"
irb(main):007:0> f.close
=> nil
irb(main):008:0> [~/tmp] cat foo
FOO
bar
BAZ
 
R

Robert Dober

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
No not necessarily, that depends how you can access the file, you can
use Ruby as an inplace filter on a file

ruby -i.bup -ne 'puts $_.gsub(/foo/,"bar")' somefile

will edit somefile and create a backup copy somefile.bup of the
original. Now when it comes to using files inside an application
things are little bit more complicated :(

HTH
Robert
 
L

Luis Parravicini

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?

There is also the '-i' argument to ruby. The example is from the man page:
================================
% echo matz > /tmp/junk
% cat /tmp/junk
matz
% ruby -p -i.bak -e "$_.upcase!" /tmp/junk
% cat /tmp/junk
MATZ
% cat /tmp/junk.bak
matz
================================
 
B

barjunk


Although it may be wrong, I recently used lockfile and temporary files
to do inline editing. It is probably inefficient, but it seems to
work for what I am using it for.

Maybe someone can point out how to use the -i feature of ruby from
within the program instead of a shell command, that would work nice
as well.

In the application I was working on, there were possibilities for
multiple access at the same time.

Mike B.
 
L

lrlebron

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

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
 
B

barjunk

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.
 
L

lrlebron

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
 
L

lrlebron

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.

Here's an example of using rio
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


I had converted a bunch of vb6 files to vb.net. The VS2005 convertor
added hundreds of comment lines that started with 'UPGRADE. I also
wanted to change the vb6 Exception handlers
On Error GoTo ErrHandler
ErrHandler
Resume Next

to the vb.net format of
Try
Catch ex as Exception
End Try

This little snippet of code did the trick very quickly.

Luis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top