From: Yossef Mendelssohn Date: 2011-10-20T07:43:16+09:00 Subject: Re: Moving files into alphabetical directories On Oct 19, 4:47 pm, Jesús Gabriel y Galán wrote: > 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 This is a good place to use Enumerable#group_by. >> names = %w(simon sadie tyler kirsty kevin keith) => ["simon", "sadie", "tyler", "kirsty", "kevin", "keith"] >> names.group_by { |n| n[0,1].upcase } => {"K"=>["kirsty", "kevin", "keith"], "S"=>["simon", "sadie"], "T"=>["tyler"]} -- -yossef