0
0
Rubyprogramming~10 mins

Dir operations for directories in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to list all files in the current directory.

Ruby
files = Dir.[1](".")
puts files
Drag options to blanks, or click blank then click option'
Aentries
Bopen
Cread
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using Dir.open returns a directory object, not an array.
Dir.read is not a valid method.
Dir.list does not exist.
2fill in blank
medium

Complete the code to change the current working directory to '/tmp'.

Ruby
Dir.[1]("/tmp")
Drag options to blanks, or click blank then click option'
Aopen
Bcd
Cchange
Dchdir
Attempts:
3 left
💡 Hint
Common Mistakes
Using Dir.open does not change directory.
Dir.cd is not a Ruby method.
Dir.change does not exist.
3fill in blank
hard

Fix the error in the code to get the current working directory.

Ruby
current_dir = Dir.[1]
Drag options to blanks, or click blank then click option'
Apwd
Bpwd()
Cgetwd
Dget_current_dir
Attempts:
3 left
💡 Hint
Common Mistakes
Dir.pwd is an alias for Dir.getwd.
Dir.pwd() is invalid syntax in Ruby.
Dir.get_current_dir does not exist.
4fill in blank
hard

Fill both blanks to create a new directory named 'test_dir' and then remove it.

Ruby
Dir.[1]("test_dir")
Dir.[2]("test_dir")
Drag options to blanks, or click blank then click option'
Amkdir
Brmdir
Cremove
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using Dir.remove or Dir.delete which do not exist.
Trying to remove a directory before creating it.
5fill in blank
hard

Fill all three blanks to list all Ruby files in the current directory using a pattern.

Ruby
ruby_files = Dir.[1]("*.[2]")
ruby_files.each do |[3]|
  puts [3]
end
Drag options to blanks, or click blank then click option'
Aglob
Brb
Cfile
Dfilename
Attempts:
3 left
💡 Hint
Common Mistakes
Using Dir.entries instead of Dir.glob for pattern matching.
Using incorrect file extension.
Using a block variable name that is not consistent.