To move a file in Ruby, we can use the FileUtils
module. The FileUtils
module provides several methods for handling files, including copying, moving, and deleting files.
To move a file using FileUtils
, we can use the mv
method. The mv
method takes two arguments: the source file path and the target file path.
Here's an example code snippet that demonstrates how to move a file in Ruby:
main.rb256 chars9 lines
In this example, we first require the fileutils
module. Then, we define the source and target file paths as variables. Finally, we call the mv
method with the source and target file paths as arguments to move the file.
Note that if the target file already exists, the mv
method will overwrite it, unless you specify the :force
option. If you want to preserve the original file attributes (such as the timestamp and permissions), you can use the :preserve
option.
gistlibby LogSnag