From: Robert Klemme Date: 2010-10-16T19:00:10+09:00 Subject: Re: Most elegant way: array of files from array of directories On 16.10.2010 06:50, Terry Michaels wrote: > I have a array of directories passed into my program. I want a new array > which is a list of all files in said directories (only one level deep is > necessary). N.B.: > > 1. Each elements of the new array must be an absolute (full) path, or at > least relative to the cwd. (Assume all directories provided are also > absolute or relative to cwd) > 2. OS and preference agnostic: Especially, I do not know what directory > separator will be used (say, backward- or forward-slash) or whether > there will be a trailing directory separator in the directory path (say, > a trailing forward-slash). You can always use a forward slash in Ruby. Duplicate slashes are not an issue either IIRC. > Dir.foreach almost does what I want, but gives relative output instead > of absolute. > > array_of_file_paths = Array.new > > array_of_directory_paths.each do |directory_path| > # ??? > end How about array_of_file_paths = array_of_directory_paths.map do |dir| Dir["#{dir}/*", "#{dir}/.*"] end.delete_if {|f| test ?d, f} ? Of course, if you do not need hidden files you can omit the second arg to Dir[]. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/