You can manipulate file directories (folders) and files from within Ruby programs using methods from the
File classes. For documentation, see http://www.ruby-doc.org/core/classes/Dir.html and http://www.ruby-doc.org/core/classes/File.html. For example, you can change directories (using an absolute path), and then store the value of the directory path in a variable as follows:
Dir.chdir( "/Users/penelope" ) home =
Dir.pwd# => "/Users/penelope/" p home # => "/Users/penelope"
If you need a directory, create it with
mkdir; later on, delete it with
delete, a synonym of
Dir.mkdir( "/Users/herman/sandbox" )
Dir.rmdir( "/Users/herman/sandbox" )
You can also set permissions (where the mask
755 sets permissions owner, group, world [anyone] to
r = read,
w = write, and
x = execute) on a new directory (not one that already exists) with
To create a new file and open it at the same time, use the
new, like this:
file = File.new( "file.rb", "w" )
The first argument names the new file, and the second argument specifies the file mode, either
r for readable,
w for writable, or
x for executable. The effects of the different modes are shown in Table 6.
Table 6. File modes
Read-only, starts at beginning of file (default mode).
Read-write, starts at beginning of file.
Write-only, truncates existing file to zero length or creates a new file for writing.