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