What is the fastest, most optimized, one-liner way to get an array of the directories (excluding files) in Ruby?
How about including files?
What is the fastest, most optimized, one-liner way to get an array of the directories (excluding files) in Ruby?
How about including files?
For list of directories try
List of files is harder, because in Unix directory is also a file, so you need to test for type or remove entries from returned list which is parent of other entries.
And for list of all files and directories simply
Fast one liner
Only directories
Directories and normal files
Pure beautiful ruby
Here's an example that combines dynamic discovery of a Rails project directory with Dir.glob:
I believe none of the solutions here deal with hidden directories (e.g. '.test'):
As noted in other answers here, you can use
Dir.glob
. Keep in mind that folders can have lots of strange characters in them, and glob arguments are patterns, so some characters have special meanings. As such, it's unsafe to do something like the following:Instead do:
Instead of
Dir.glob(foo)
you can also writeDir[foo]
(howeverDir.glob
can also take a block, in which case it will yield each path instead of creating an array).Ruby Glob Docs