# Most elegant way: array of files from array of directories

**URL:** <https://www.ruby-forum.com/t/most-elegant-way-array-of-files-from-array-of-directories/197571>\
**Category:** Ruby\
**Created:** [October 16, 2010, 6:50am UTC](https://www.ruby-forum.com/t/most-elegant-way-array-of-files-from-array-of-directories/197571 "2010-10-16T06:50:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![okkezSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/85f322/32.png) [@okkezSS](https://www.ruby-forum.com/u/okkezSS)\
**Post date:** [October 16, 2010, 6:50am UTC](https://www.ruby-forum.com/t/most-elegant-way-array-of-files-from-array-of-directories/197571/1 "2010-10-16T06:50:27Z")

</div>

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).

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

---

<div class="post-metadata">

**Author:** ![callyworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@callyworld](https://www.ruby-forum.com/u/callyworld)\
**Post date:** [October 16, 2010, 11:08am UTC](https://www.ruby-forum.com/t/most-elegant-way-array-of-files-from-array-of-directories/197571/2 "2010-10-16T11:08:37Z")

</div>

On Sat, Oct 16, 2010 at 6:50 AM, Terry M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## a trailing forward-slash).
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Probably not the most elegant way, but this can give you some ideas:

irb(main):032:0\> Dir[“_"].each do |f|  
irb(main):033:1_ next if test ?f,f # skip the files  
irb(main):034:1\> puts Dir[”#{f}/\*"].map {|x| File.expand\_path(x)}  
irb(main):035:1\> end

You can loop around this collecting the subarrays and then flatten.

Jesus.

---

<div class="post-metadata">

**Author:** ![callyworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@callyworld](https://www.ruby-forum.com/u/callyworld)\
**Post date:** [October 16, 2010, 12:00pm UTC](https://www.ruby-forum.com/t/most-elegant-way-array-of-files-from-array-of-directories/197571/3 "2010-10-16T12:00:47Z")

</div>

On 16.10.2010 06:50, Terry M. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![callyworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@callyworld](https://www.ruby-forum.com/u/callyworld)\
**Post date:** [October 17, 2010, 12:41am UTC](https://www.ruby-forum.com/t/most-elegant-way-array-of-files-from-array-of-directories/197571/4 "2010-10-17T00:41:41Z")

</div>

On Oct 15, 11:50pm, Terry M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## a trailing forward-slash).
> 
> Posted viahttp://www.ruby-forum.com/.

dir\_paths.map{|d| Dir[File.join( d, “\*”)] }.flatten.  
select{|f| test ?f, f}

---

<div class="post-metadata">

**Author:** ![callyworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@callyworld](https://www.ruby-forum.com/u/callyworld)\
**Post date:** [October 16, 2010, 5:51pm UTC](https://www.ruby-forum.com/t/most-elegant-way-array-of-files-from-array-of-directories/197571/5 "2010-10-16T17:51:48Z")

</div>

On Saturday, October 16, 2010 05:00:10 am Robert K. wrote:

> > 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.

Also worth mentioning: I don’t know of any OSes Ruby supports which  
don’t also  
support a forward Slash. For example, while Windows typically uses  
backslashes, it also supports forward slashes.
