On Wednesday 04 May 2005 14:28, Joost Diepenmaat wrote:
> On Wed, May 04, 2005 at 09:08:19PM +0900, Ralf Mler wrote:
> > Hi,
> >
> > i simply want so change an existing file
>
> [ ... ]
>
> > irb(main):012:0> file = File.open('test');  file.each do |line|
> > line.gsub!(/\d+/,'###') end
>
> AFAIK line is a String so changing it won't automatically change the
> file. There's a reason, too:
[...]

# open file test for reading
open "test" do |src|
    # open test.tmp for writing
    open "test.tmp", "w" do |tmp|
        # read line from test, substitute and write modified line to test.tmp
        src.each { |line| tmp << line.gsub(/\d+/, '###') }
    end
end
File.rename "test.tmp", "test"

Stefan