delete a csv file in ruby

To delete a file in Ruby, you need to use the File.delete or File.unlink method. To delete a CSV file, simply call one of these methods with the path of the file:

main.rb
require 'fileutils'

File.delete('path/to/file.csv')
# or
File.unlink('path/to/file.csv')
90 chars
6 lines

If you want to ensure that the file exists before attempting to delete it, you can use the File.exist? method:

main.rb
if File.exist?('path/to/file.csv')
  File.delete('path/to/file.csv')
end
73 chars
4 lines

Alternatively, you can use the FileUtils module, which provides a shorthand method FileUtils.rm for deleting files:

main.rb
require 'fileutils'

FileUtils.rm('path/to/file.csv')
54 chars
4 lines

Note that this method also allows you to delete multiple files at once, and has additional options for recursive deletion and error handling.

gistlibby LogSnag