We use directory operations to look inside folders, create new folders, or remove them. This helps us organize and manage files on the computer.
Dir operations for directories in Ruby
Dir.entries(path) Dir.mkdir(path) Dir.exist?(path) Dir.rmdir(path) Dir.chdir(path) do # code inside new directory end
Dir.entries(path) returns an array of all files and folders inside the given path.
Dir.mkdir(path) creates a new directory at the given path.
files = Dir.entries(".") puts files
Dir.mkdir("new_folder") puts Dir.exist?("new_folder")
Dir.rmdir("old_folder")Dir.chdir("new_folder") do puts Dir.pwd end
This program shows files in the current folder, creates a new folder if it doesn't exist, changes into it to list files, then removes the folder.
puts "Current directory files:" puts Dir.entries(".") folder_name = "test_dir" unless Dir.exist?(folder_name) Dir.mkdir(folder_name) puts "Created directory: #{folder_name}" else puts "Directory already exists: #{folder_name}" end puts "Changing to #{folder_name} and listing files:" Dir.chdir(folder_name) do puts Dir.entries(".") end Dir.rmdir(folder_name) puts "Removed directory: #{folder_name}"
Dir.entries always includes "." (current folder) and ".." (parent folder) in the list.
You can only remove empty directories with Dir.rmdir. To remove folders with files, you need other methods.
Use Dir.chdir with a block to safely change directories and return back automatically.
Use Dir.entries to see files and folders inside a directory.
Create folders with Dir.mkdir and check existence with Dir.exist?.
Remove empty folders with Dir.rmdir and change directories with Dir.chdir.
Practice
. and ..?Solution
Step 1: Understand the purpose of
This method returns an array of all entries in the directory, including special entriesDir.entries.and...Step 2: Compare with other methods
Dir.mkdircreates a directory,Dir.exist?checks if a directory exists, andDir.chdirchanges the current directory. None of these list contents.Final Answer:
Dir.entries -> Option DQuick Check:
List directory contents = Dir.entries [OK]
- Confusing Dir.entries with Dir.mkdir
- Using Dir.exist? to list files
- Thinking Dir.chdir lists directory contents
test_folder in Ruby?Solution
Step 1: Recall the method to create directories
The correct Ruby method to create a directory isDir.mkdir.Step 2: Check syntax correctness
Dir.mkdir('test_folder')correctly creates a directory named 'test_folder'. Other options are invalid methods.Final Answer:
Dir.mkdir('test_folder') -> Option AQuick Check:
Create directory = Dir.mkdir [OK]
- Using Dir.create instead of Dir.mkdir
- Trying Dir.new for directory creation
- Misspelling method names
Dir.mkdir('sample_dir') unless Dir.exist?('sample_dir')
entries = Dir.entries('sample_dir')
puts entries.inspectSolution
Step 1: Check directory creation and existence
The code creates 'sample_dir' if it does not exist, so the directory will exist before listing entries.Step 2: Understand Dir.entries output for empty directory
An empty directory still contains special entries:.(current directory) and..(parent directory). So,Dir.entriesreturns [".", ".."].Final Answer:
[".", ".."] -> Option CQuick Check:
Empty directory entries = [".", ".."] [OK]
- Expecting empty array for empty directory
- Thinking directory name appears in entries
- Assuming error if directory just created
Dir.rmdir('my_folder')
puts 'Folder removed'Solution
Step 1: Understand Dir.rmdir behavior
Dir.rmdirremoves only empty directories. If 'my_folder' contains files or folders, it will raise an error.Step 2: Check other options for correctness
Dir.rmdir only removes non-empty directories is wrong because it says the opposite. Dir.rmdir should be Dir.remove is invalid method name. Dir.rmdir needs a block to execute is incorrect; no block needed.Final Answer:
Dir.rmdir requires the directory to be empty before removal -> Option BQuick Check:
Dir.rmdir removes empty dirs only [OK]
- Trying to remove non-empty directories with Dir.rmdir
- Using wrong method name like Dir.remove
- Expecting Dir.rmdir to accept a block
projects, create a new folder new_app inside it, and then list the contents of projects. Which sequence of Ruby commands correctly does this?Solution
Step 1: Change directory to 'projects'
UsingDir.chdir('projects')sets the current directory to 'projects'.Step 2: Create 'new_app' inside 'projects'
Dir.mkdir('new_app')creates the new folder inside the current directory.Step 3: List contents of current directory
Dir.entries('.')lists all files and folders inside 'projects', including 'new_app'.Final Answer:
Dir.chdir('projects') Dir.mkdir('new_app') puts Dir.entries('.') -> Option AQuick Check:
Change dir, create folder, list contents = Dir.chdir('projects') Dir.mkdir('new_app') puts Dir.entries('.') [OK]
- Creating folder before changing directory
- Listing wrong directory contents
- Changing to new folder before creating it
Ready to prove it?
Pass the test and this pattern moves unknown
