From: "Jesús Gabriel y Galán" Date: 2011-10-20T05:47:47+09:00 Subject: Re: Moving files into alphabetical directories On Wed, Oct 19, 2011 at 9:53 PM, simon harrison wrote: > Hi. Let's say I have an array like this: > > irb(main):008:0> letters > => ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", > "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] > > and another array like this: > > irb(main):001:0> names = %w(simon sadie tyler kirsty kevin keith) > => ["simon", "sadie", "tyler", "kirsty", "kevin", "keith"] > > > I'd like to end up with either an array or a hash that contains all > letters of the alphabet (letters) with the matching files (names) and > maybe nil for the letters with no value. I hope this makes sense. By matching file names you mean the ones starting with that letter? A way to get a hash that contains a key for each letter with an array of words starting with that letter: h = {} names.each do |name| letter = name[0,1].upcase #seems the letters must be upcased (h[letter] ||= []) << name end For the letters that don't get any word, it depends what you want to do with that. In this solution you will get nil if you try to access it. > As per the subject, this is initially wanted for the purposes of moving > files into directories. However, as FileUtils.mv happily takes an array > (or a hash?), it'd be nice to know how to do this generally. Then you can iterate the hash and call FileUtils.mv for each directory like this: h.each {|directory, files| FileUtils.mv files, directory} Hope this helps, Jesus.