Refactored directory.rb using array and loop