Implement inreplace natively in Ruby

I found yet another instance where the escaping wasn't perfect, so got fed up and just did it in Ruby. I hope this works for all existing usage. It should.

The bonus here is that you can use RegExps now.
This commit is contained in:
Max Howell 2009-10-23 19:01:05 +01:00
parent 7375758c9a
commit e289164adc

View File

@ -149,13 +149,8 @@ end
# replaces before with after for the file path # replaces before with after for the file path
def inreplace path, before, after def inreplace path, before, after
before=Regexp.escape before.to_s f = File.open(path, 'r')
before.gsub! "/", "\\/" # I guess not escaped as delimiter varies o = f.read.gsub(before, after)
after=after.to_s f.reopen(path, 'w').write(o)
after.gsub! "\\", "\\\\" f.close
after.gsub! "/", "\\/"
after.gsub! "$", "\\$"
# FIXME use proper Ruby for teh exceptions!
safe_system "/usr/bin/perl", "-pi", "-e", "s/#{before}/#{after}/g", path
end end