Class: File

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

Class Method Summary collapse

Class Method Details

.write_with_backup(path, content) ⇒ Object

Equivalent to File::open with an associated block, but moves any existing file with the same name to the side first. Returns renamed path or false if none.



858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
# File 'lib/inline.rb', line 858

def self.write_with_backup(path, content)
  renamed = false

  if File.file? path then
    renamed = "#{path}.old"

    begin
      File.rename path, renamed
    rescue SystemCallError # in case of race condition
      # do nothing
    end
  end

  File.write path, content

  renamed
end