Skip to content

Instantly share code, notes, and snippets.

@adparadise
Created December 8, 2011 18:21
Show Gist options
  • Save adparadise/1447917 to your computer and use it in GitHub Desktop.
Save adparadise/1447917 to your computer and use it in GitHub Desktop.
Replace a file with altered contents
file = File.new(filename)
contents = file.read
newfile = Tempfile.new(filename)
begin
contents.each do |line|
newfile.write("// " + line)
end
newfile.flush
File.rename(newfile.path, file.path)
ensure
newfile.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment