[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

need help with reading/writing to files

Ben

1/17/2005 5:08:00 AM

I've been trying to write a program that will take a file, do some
find/replace operation and save the file. I'm pretty new to ruby, and I
can't seem to figure out how to do this succinctly. This is my latest
attempt, but it seems like I shouldn't need to open the file twice.

I tried to just open the file with "r+" permissions, but if I overwrote
it with a string that was shorter than the original contents, the extra
characters would stick around. I couldn't figure out how to slice off
the extra stuff I didn't need.

Any suggestions on how to reduce this to one call to File.open???
Thanks! Any other suggestions are also appreciated. Here's the code...

print "pattern:"
pattern = STDIN.gets.chop
print "replacement:"
replacement = STDIN.gets.chop
str = ""
ARGV.each do |file|
File.open(file,"r+") do |handle|
str = handle.read
end
File.open(file,"w+") do |handle|
str.gsub!(/#{pattern}/,replacement)
handle.write(str)
end
end



Thanks,

Ben

4 Answers

Mark Hubbart

1/17/2005 7:58:00 AM

0

On Mon, 17 Jan 2005 14:11:07 +0900, Ben <bhbrinckerhoff@wustl.edu> wrote:
> I've been trying to write a program that will take a file, do some
> find/replace operation and save the file. I'm pretty new to ruby, and I
> can't seem to figure out how to do this succinctly. This is my latest
> attempt, but it seems like I shouldn't need to open the file twice.
>
> I tried to just open the file with "r+" permissions, but if I overwrote
> it with a string that was shorter than the original contents, the extra
> characters would stick around. I couldn't figure out how to slice off
> the extra stuff I didn't need.
>
> Any suggestions on how to reduce this to one call to File.open???
> Thanks! Any other suggestions are also appreciated. Here's the code...
>
> print "pattern:"
> pattern = STDIN.gets.chop
> print "replacement:"
> replacement = STDIN.gets.chop
> str = ""
> ARGV.each do |file|
> File.open(file,"r+") do |handle|
> str = handle.read
> end
> File.open(file,"w+") do |handle|
> str.gsub!(/#{pattern}/,replacement)
> handle.write(str)
handle.truncate(handle.pos)
> end
> end

File#truncate(size) truncates the file to the given size. Passing your
current position after you write to the file truncates it immediately
after the last character you wrote.

HTH,
Mark


Glenn Parker

1/17/2005 4:49:00 PM

0

Ben wrote:
> I've been trying to write a program that will take a file, do some
> find/replace operation and save the file. I'm pretty new to ruby, and I
> can't seem to figure out how to do this succinctly. This is my latest
> attempt, but it seems like I shouldn't need to open the file twice.

Have you considered doing it all from the command line?

% ruby -pi -e 'gsub /pattern/, "replacement"' file1 file2...

--
Glenn Parker | glenn.parker-AT-comcast.net | <http://www.tetrafoi...


Ben

1/17/2005 5:26:00 PM

0

File#truncate was exactly what I was looking for. Thanks! Also, thanks
for the suggestion about the command line, I might try that as well,
but I might be making the script a little more complicated so it might
be a bit much for the command line. Thanks, both! I appreciate it.

Ben

Major Debacle

6/8/2008 4:24:00 PM

0

kT wrote:
> Major Debacle wrote:
>> Raymond wrote:
>>> Invade Brazil Now
>>> Bomb, bomb, bomb Rio de Janeiro
>>>
>>> Brazil Oil Field May Hold 8B Barrels
>>> Brazil's Petrobras Estimates Up to 8 Billion Barrels of Light Oil in
>>> Offshore Field
>>> By ALAN CLENDENNING
>>> AP Business Writer
>>> SAO PAULO, Brazil November 8, 2007 (AP) The Associated Press
>>>
>>> RSS Brazil's state oil company said that it has discovered as much as
>>> 8 billion barrels of light crude in an ultra-deep field off the coast
>>> of Rio de Janeiro, an amount that could help transform the country
>>> into a major world oil exporter
>>>
>>> http://abcnews.go.com/Business/wireStory?...
>> 8 billion barrels?
>>
>> Pfff...
>>
>> The world rips through 8 billion barrels of crude every 100 days.
>
> And your beloved United States Military is killing numerous babies
> worldwide in order to supply you with that oil. I guess that makes you a
> baby killer as well, fuckwit. Until you admit to yourself that you are a
> lowlife baby killer for oil, you will be unable to address the problems
> as a rational adult. It's your government. You elected it to represent
> YOU. You need to start behaving like a MAN and not a baby killing robot.

You're funny... why don't you go spit on some troops.

--
Good Earth Original Herbal Tea contains *Artificial Flavoring*