Skip to content

Instantly share code, notes, and snippets.

@alxschwarz
Created July 17, 2012 11:02
Show Gist options
  • Save alxschwarz/3128734 to your computer and use it in GitHub Desktop.
Save alxschwarz/3128734 to your computer and use it in GitHub Desktop.
String replace in file
def string_replace(init_string, replace_string, file_name)
file = File.new(file_name)
lines = file.readlines
file.close
changes = false
x = 0
lines.each do |line|
changes = true if line.sub!(init_string, replace_string)
end
if changes
file = File.new(file_name,'w')
lines.each do |line|
file.write(line)
end
file.close
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment