Ruby Tutorial
Ruby CGI
Ruby Advanced
The Dir
class in Ruby represents directories in your file system. It provides several class methods for handling directories.
1. Create a directory
The mkdir
method can be used to create a new directory:
Dir.mkdir('new_directory')
2. Delete a directory
The delete
or rmdir
method can be used to delete a directory:
Dir.delete('directory_to_delete') # or Dir.rmdir('directory_to_delete')
Note that the directory must be empty to delete it.
3. Change the current directory
You can change the current directory using the chdir
or cd
method:
Dir.chdir('/path/to/new/directory') # or Dir.cd('/path/to/new/directory')
4. List directories and files
The entries
method returns an array of filenames in a directory:
files = Dir.entries('/path/to/directory') files.each do |file| puts file end
You can also use the glob
method to match filenames using a pattern:
files = Dir.glob('*.txt') # Get all .txt files in current directory files.each do |file| puts file end
5. Get the current directory
The pwd
or getwd
method returns the path of the current directory:
puts Dir.pwd # or puts Dir.getwd
6. Working with temporary directories
The mktmpdir
method in Dir::Tmpname
module creates a temporary directory:
require 'tmpdir' temp_dir = Dir.mktmpdir puts temp_dir # Outputs: path to a newly created temporary directory
Remember to delete the temporary directory once you're done with it.
7. Iterate over directories and files
The foreach
method executes a block of code for each entry in a directory:
Dir.foreach('/path/to/directory') do |file| puts file end
Note: The .
and ..
entries represent the current directory and parent directory respectively.
These are just a few of the most commonly used methods in the Dir
class. For more advanced usage, refer to the Ruby documentation.
List files in a directory with Ruby Dir class:
Dir.entries
method can be used to list all files and directories in a given directory.files = Dir.entries('/path/to/directory') puts files
Ruby Dir class example:
Dir
class provides methods for working with directories in Ruby.# List files in the current directory files = Dir.entries('.') puts files
Working with directories in Ruby:
Dir
class provides methods for working with directories, such as creating, removing, or checking if a directory exists.# Create a directory Dir.mkdir('my_directory') # Remove a directory Dir.rmdir('my_directory') # Check if a directory exists if Dir.exist?('my_directory') puts 'The directory exists.' else puts 'The directory does not exist.' end
Ruby Dir glob method:
Dir.glob
method allows you to retrieve files matching a specific pattern.# List all Ruby files in the current directory ruby_files = Dir.glob('*.rb') puts ruby_files
How to navigate directories in Ruby:
Dir.chdir
method to change the current working directory.# Change to a different directory Dir.chdir('/path/to/new_directory') # Print the current working directory puts Dir.pwd
Recursively list files with Ruby Dir:
Dir.glob
with File.join
to recursively list files in subdirectories.# Recursively list all Ruby files in the current directory and subdirectories all_ruby_files = Dir.glob('**/*.rb', base: '.') puts all_ruby_files
Filtering files with Ruby Dir class:
select
to filter files based on specific criteria.# List only files (excluding directories) in the current directory files_only = Dir.entries('.').select { |f| File.file?(f) } puts files_only
Sorting files in a directory with Ruby:
sort
or sort_by
to sort files in a directory.# List and sort all files in the current directory sorted_files = Dir.entries('.').sort puts sorted_files
Getting the current working directory in Ruby:
Dir.pwd
method returns the current working directory.current_directory = Dir.pwd puts "Current working directory: #{current_directory}"
Changing directories in Ruby:
Dir.chdir
to change the current working directory.# Change to a different directory Dir.chdir('/path/to/new_directory')
Checking if a directory exists with Ruby Dir:
Dir.exist?
method checks if a directory exists.if Dir.exist?('my_directory') puts 'The directory exists.' else puts 'The directory does not exist.' end
Using Dir class for file operations in Ruby:
Dir
class provides methods for various file operations, such as creating, removing, or listing files in a directory.# List all files in the current directory files = Dir.entries('.') puts files # Create a new directory Dir.mkdir('new_directory') # Remove a directory Dir.rmdir('new_directory')
Iterating through files in a directory using Ruby Dir:
Dir.foreach
to iterate through files in a directory.# Iterate through files in the current directory Dir.foreach('.') do |file| puts file end