Xan Xann wrote:

> Hi,
> 
> Can anyone help me in that?
> I just want to write the following function:
> 
> substitute(f,g,w,x)
> 
> where f and g are files and w and x are strings
> That function substitutes all the ocurrences of w in the file f and
> writes x to g

substitute(source,dest,find,replace)
  data = File.read(source)
  result = data.gsub(/#{find}/,#{replace})
  File.open(dest) { |f| f.write(result) } if data != result
end

> I thinked to pass file to string but I thinked it was not very fast.
> Better directly

Not necessarily. Putting a file's contents into a string is the best way to
carry out substitutions, especially when regular expressions are involved.

If the files are large, one can write a line-by-line, therefore streaming,
solution, from one file to another, but this also has limitations when
sophisticated regular expressions are required.

-- 
Paul Lutus
http://www.arachnoid.com