Class: File

Inherits:
Object
  • Object
show all
Defined in:
lib/inline.rb

Class Method Summary collapse

Class Method Details

.write_with_backup(path) ⇒ Object

Equivalent to File::open with an associated block, but moves any existing file with the same name to the side first.



872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
# File 'lib/inline.rb', line 872

def self.write_with_backup(path) # returns true if file already existed

  # move previous version to the side if it exists
  renamed = false
  if File.file? path then
    begin
      File.rename path, path + ".old"
      renamed = true
    rescue SystemCallError
      # do nothing
    end
  end

  File.open(path, "w") do |io|
    yield(io)
  end

  return renamed
end