views:

62

answers:

1

I want to grab a list of all the files under a particular directory. Dir.glob works great, but there doesn't seem to be a way to limit results to just files (excluding directories).

Heres's what I have right now:

files = Dir.glob('my_dir/**/*').reject { |f| File.directory?(f) }

Is there a more elegant way to accomplish this?

+2  A: 

That's actually a fairly efficient way to go about doing it, but you might also use the Find module:

require 'find'

found = [ ]

Find.find(base_path) do |path|
  found << path if (File.file?(path))
end
tadman
+1: I always forget about the `find` library, but it is indeed the most efficient (in terms of programmer time, anyway) way to port a POSIX `find` expression to Ruby.
Jörg W Mittag