Concept Flow - Dir operations for directories
Start
Open Directory
Read Entries
Process Each Entry
Close Directory
End
This flow shows how Ruby opens a directory, reads its entries one by one, processes them, and then closes the directory.
Dir.open("./") do |dir| dir.each do |entry| puts entry end end
| Step | Action | Directory State | Entry Read | Output |
|---|---|---|---|---|
| 1 | Open directory './' | Directory opened, ready to read | ||
| 2 | Read first entry | Entries unread: all | "." | Prints '.' (current directory) |
| 3 | Read second entry | Entries unread: all except '.' | ".." | Prints '..' (parent directory) |
| 4 | Read third entry | Entries unread: all except '.' and '..' | "file1.txt" | Prints 'file1.txt' |
| 5 | Read fourth entry | Entries unread: all except '.', '..', 'file1.txt' | "subdir" | Prints 'subdir' |
| 6 | No more entries | All entries read | End loop | |
| 7 | Close directory | Directory closed |
| Variable | Start | After Step 2 | After Step 3 | After Step 4 | After Step 5 | Final |
|---|---|---|---|---|---|---|
| entry | nil | "." | ".." | "file1.txt" | "subdir" | nil |
Dir operations in Ruby:
- Use Dir.open(path) { |dir| ... } to open directory safely
- Use dir.each to read entries one by one
- Entries include '.' (current) and '..' (parent)
- Directory auto-closes after block ends
- Useful for processing files step-by-step. and ..?Dir.entries. and ...Dir.mkdir creates a directory, Dir.exist? checks if a directory exists, and Dir.chdir changes the current directory. None of these list contents.test_folder in Ruby?Dir.mkdir.Dir.mkdir('test_folder') correctly creates a directory named 'test_folder'. Other options are invalid methods.Dir.mkdir('sample_dir') unless Dir.exist?('sample_dir')
entries = Dir.entries('sample_dir')
puts entries.inspect. (current directory) and .. (parent directory). So, Dir.entries returns [".", ".."].Dir.rmdir('my_folder')
puts 'Folder removed'Dir.rmdir removes only empty directories. If 'my_folder' contains files or folders, it will raise an error.projects, create a new folder new_app inside it, and then list the contents of projects. Which sequence of Ruby commands correctly does this?Dir.chdir('projects') sets the current directory to 'projects'.Dir.mkdir('new_app') creates the new folder inside the current directory.Dir.entries('.') lists all files and folders inside 'projects', including 'new_app'.Pass the test and this pattern moves unknown