0
0
Rubyprogramming~20 mins

Dir operations for directories in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Dir Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Ruby code using Dir.entries?
Consider the following Ruby code snippet that lists entries in the current directory. What will it output?
Ruby
puts Dir.entries(".").sort
AOnly files in the current directory sorted alphabetically
BOnly directories in the current directory sorted alphabetically
CAn array of all files and directories excluding '.' and '..' sorted alphabetically
DAn array of all files and directories including '.' and '..' sorted alphabetically
Attempts:
2 left
💡 Hint
Dir.entries returns all entries including special ones '.' and '..'.
Predict Output
intermediate
2:00remaining
What does Dir.glob("*.rb") return?
Given a directory with files: test.rb, example.py, script.rb, and README.md, what will Dir.glob("*.rb") return?
Ruby
p Dir.glob("*.rb")
A["test.rb", "script.rb"]
B["test.rb", "example.py", "script.rb"]
C["README.md"]
DAn empty array []
Attempts:
2 left
💡 Hint
Dir.glob with '*.rb' matches files ending with .rb only.
🔧 Debug
advanced
2:00remaining
Why does this code raise an error?
This Ruby code tries to change directory and list files but raises an error. Identify the cause.
Ruby
Dir.chdir("nonexistent_dir")
puts Dir.entries(".")
AIt raises Errno::ENOENT because the directory 'nonexistent_dir' does not exist
BIt raises TypeError because Dir.chdir expects a block
CIt raises NoMethodError because Dir.entries is called incorrectly
DIt raises ArgumentError because Dir.chdir requires absolute path
Attempts:
2 left
💡 Hint
Check if the directory exists before changing into it.
📝 Syntax
advanced
2:00remaining
Which option correctly creates a new directory named 'data'?
Select the Ruby code that correctly creates a directory named 'data' in the current working directory.
ADir.make('data')
BDir.mkdir('data')
CDir.create('data')
DDir.new('data')
Attempts:
2 left
💡 Hint
The method to create a directory is mkdir.
🚀 Application
expert
2:00remaining
How many entries will this code print?
Given a directory containing 3 files and 2 subdirectories (excluding '.' and '..'), what will this Ruby code print?
Ruby
entries = Dir.entries(".") - ['.', '..']
puts entries.size
A7
B3
C5
D2
Attempts:
2 left
💡 Hint
Dir.entries includes '.' and '..', which are removed here.