I have used gsub previously for a regex match, but what should I call for string literals?
I want to replace pair[0] with pair[1] wherever pair[0] is found in the file.
text = File.read( fname )
@hash_old_to_new.each do
|pair|
puts "\tReplacing " + pair[0] + " with " + pair[1]
# result = text.gsub( /pair[0]/, pair[1] ) <--- this is no good
end
File.open( fname, "w" ) { |file| file << result }
gsubalso works for string literals.Note that
gsubreturns a new String, rather than modifying the existing String “in place”. Because of the way your code is written, this will cause you to lose updates. You can usegsub!, or else you can chain calls like this: