From: Jan Svitok Date: 2006-11-24T07:28:48+09:00 Subject: Re: double-slashes in input causing trouble or you can use references: old_section = $1 new_body = body.sub(old_section, bit_at_top + '\&') \& = the last match. if there was gsub instead of sub, this would be slower as the replacement takes place on every occurence. In this case, however, there's max 1 occurence. You can do as well: - body = "" - File.open(input, 'r') do |file| - body = file.read - end + body = File.read(input) and File.open(input,'w') do |file| file.write new_body - end + end unless new_body == body