# File fun! How would you

**URL:** <https://www.ruby-forum.com/t/file-fun-how-would-you/120896>\
**Category:** Ruby\
**Created:** [November 21, 2007, 4:01pm UTC](https://www.ruby-forum.com/t/file-fun-how-would-you/120896 "2007-11-21T16:01:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Casimir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@Casimir](https://www.ruby-forum.com/u/Casimir)\
**Post date:** [November 21, 2007, 4:01pm UTC](https://www.ruby-forum.com/t/file-fun-how-would-you/120896/1 "2007-11-21T16:01:42Z")

</div>

…index in a hash…  
…all directories under ‘src’…  
…that contain filetypes in ‘ft[]’…  
…without getting lost in arrays inside arrays…

#Here is my try  
src="/"  
tmpindex = []  
filetypes = [".jpg", “.png”, “.gif”, “.bmp”]  
filetypes.each { |typ| tmpindex \<\< Dir[src+"\*\*/\*"+typ] }

index={}  
tmpindex.each { |fpath|  
dirpath = File.dirname(File.expand\_path(fpath.to\_s))  
index[dirpath] = File.basename(fpath.to\_s)  
}  
puts index  
#bleed  
#end

---

<div class="post-metadata">

**Author:** ![Casimir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@Casimir](https://www.ruby-forum.com/u/Casimir)\
**Post date:** [November 21, 2007, 4:25pm UTC](https://www.ruby-forum.com/t/file-fun-how-would-you/120896/2 "2007-11-21T16:25:47Z")

</div>

On Nov 21, 9:00 am, Casimir [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> …index in a hash…  
> …all directories under ‘src’…  
> …that contain filetypes in ‘ft[]’…  
> …without getting lost in arrays inside arrays…
> 
> #Here is my try  
> src=“/”  
> tmpindex = []  
> filetypes = [“.jpg”, “.png”, “.gif”, “.bmp”]

require ‘find’

src = “/”  
index = []  
filetypes = [“.jpg”, “.png”, “.gif”, “.bmp”]

Find.find(src) do |path|  
index \<\< path if filetypes.include? File.extname(path)  
end

---

<div class="post-metadata">

**Author:** ![Casimir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@Casimir](https://www.ruby-forum.com/u/Casimir)\
**Post date:** [November 21, 2007, 7:40pm UTC](https://www.ruby-forum.com/t/file-fun-how-would-you/120896/3 "2007-11-21T19:40:28Z")

</div>

On 21.11.2007 16:00, Casimir wrote:

> index={}  
> tmpindex.each { |fpath|  
> dirpath = File.dirname(File.expand\_path(fpath.to\_s))  
> index[dirpath] = File.basename(fpath.to\_s)  
> }  
> puts index  
> #bleed  
> #end

Are you sure you want to remember only one file per directory? If not:

EXTENSIONS = %w{jpg png gif bmp}

index = Hash.new {|h,k| h[k]=[]}

Dir["\*\*/\*.{#{EXTENSIONS.join(’,’)}}"].each do |f|  
dir, base = File.split f  
index[dir] \<\< base  
end

And, for the record, the solution with #inject:

EXTENSIONS = %w{jpg png gif bmp}

index = Dir["\*\*/\*.{#{EXTENSIONS.join(’,’)}}"].inject(  
Hash.new {|h,k| h[k]=[]}) do |h,f|  
dir, base = File.split f  
h[dir] \<\< base  
h  
end

Cheers

robert

---

<div class="post-metadata">

**Author:** ![Casimir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@Casimir](https://www.ruby-forum.com/u/Casimir)\
**Post date:** [November 22, 2007, 9:36am UTC](https://www.ruby-forum.com/t/file-fun-how-would-you/120896/4 "2007-11-22T09:36:29Z")

</div>

Thanks for replies yermej and Robert! I learn some new agin! IT’s easy!  
8)

More comments further down the post… And questions.

> On Nov 21, 9:00 am, Casimir [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > …index in a hash…  
> > …all directories under ‘src’…  
> > [snip!]

yermej wrote this solution:

> require ‘find’
> 
> src = “/”  
> index = []  
> filetypes = [“.jpg”, “.png”, “.gif”, “.bmp”]
> 
> Find.find(src) do |path|  
> index \<\< path if filetypes.include? File.extname(path)  
> end

Well it works great but the results are in an array and would require  
further processing before files would be in an hash… with path as keys  
(which my orig post didn’t specify though).

Robert K. wrote:

> EXTENSIONS = %w{jpg png gif bmp}
> 
> index = Hash.new {|h,k| h[k]=[]}
> 
> Dir[“\*\*/\*.{#{EXTENSIONS.join(‘,’)}}”].each do |f|  
> dir, base = File.split f  
> index[dir] \<\< base  
> end

Wow. That is snappy.

I, too, was annoyed when I couldnt assign stuff into the Hash with \<\<.

I dont know why. It has more characters than “arr[key]=val”. You could  
save almost 2 lines with the = version. It looks better I guess. I  
couldnt bend ruby like that tho, over. Haha!

I also like how you handle the assignment of the split file path with  
“dir, base = File.split f”.

And I have no idea what the {#{EXTENSIONS.join(‘,’)}} is all about! IRB  
just gave me \*'s.

lol I am just a dummy! Hahaha!

---

<div class="post-metadata">

**Author:** ![Casimir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@Casimir](https://www.ruby-forum.com/u/Casimir)\
**Post date:** [November 22, 2007, 5:15pm UTC](https://www.ruby-forum.com/t/file-fun-how-would-you/120896/5 "2007-11-22T17:15:40Z")

</div>

On Nov 22, 2:30 am, Casimir [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end
> 
> Well it works great but the results are in an array and would require  
> further processing before files would be in an hash… with path as keys  
> (which my orig post didn’t specify though).

Sorry, missed the hash part. Borrowing from Robert’s solution:

require ‘find’

src = ‘/’  
filetypes = [“.jpg”, “.png”, “.gif”, “.bmp”]  
index = Hash.new {|h, k| h[k] = []}

Find.find(src) do |path|  
next unless filetypes.include? File.extname(path)  
dir, base = File.split path  
index[dir] \<\< base  
end

Jeremy
