It's better to use fileutils
, if it exists.. this might be good only for some text files.
def mv(source, destination) cp(source, destination) File.delete(source) end def cp(source, destination) output = [] File.open(source, 'r+').each { |line| output << line.chomp } # If the destination file doesn't exist, make it: if File.exists?(destination) == false then File.open(destination, 'w+') do |f| # open for update f.print "" # write out nothing end # automatically closed end File.open(destination, 'w') { |f| # prepare it for overwriting content # Output the right number of lines, converting the array into strings again. f.print(output.join("\n")) } # automatically closed end
Last updated 2016-12-27 at 21:26:05