Skip to content

Instantly share code, notes, and snippets.

@juangesino
Created September 1, 2015 10:21
Show Gist options
  • Save juangesino/7eb63fcd4e5bc6921f5d to your computer and use it in GitHub Desktop.
Save juangesino/7eb63fcd4e5bc6921f5d to your computer and use it in GitHub Desktop.
Get files from a directory recursively (get files inside directories).
awesome_files = [] # This will contain all the files we wanted
directories = ['app/models/'] # Initial directory to search
directories.each do |directory| # Iterate through the directories
files = Dir.glob("#{directory}/**") # Get all files and directories inside that directory
files.each do |file| # Iterate
next if file == '.' or file == '..' # Ignore . and ..
if File.directory?(file) # Check if the file is a directory or just a file
directories << file # If it's a directory, push it into the directories array to search inside it later
else
awesome_files << file # If it's a file save it
end
end
end
awesome_files # Return all saved files
# Files will come in the form of /app/models/path/to/file/user.rb
# If you want the Class you can:
# class_name = awesome_files[0].gsub!(/.*?(?=\/)/im, "").gsub!('/', '').gsub!('.rb', '').classify
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment