Paul Lutus wrote:
> #!/usr/bin/ruby
> 
> def substitute(source,dest,find,replace)
>   data = File.read(source)
>   result = data.gsub(/#{find}/,replace)
>   File.open(dest,"w") { |f| f.write(result) } if data != result
> end

The equality test there is probably unnecessary.
What you could do for a bit lower a cost, if you
really want to avoid the possible write overhead:

# Untested too
require 'fileutils'

def FileUtils.s(source, dest, subs = {})
  data = File.read source
  search = /#{subs.keys.map {|k| Regexp.escape k}.join '|'}/
  result = data.gsub(search) {|found| subs[found]}
  File.open(dest, 'w') {|f| f.write result} if $&  # Regex matched
end

FileUtils.s 'src.txt', 'dest.txt', 'foo' => 'bar'



-- 
Posted via http://www.ruby-forum.com/.