From: "Jesús Gabriel y Galán" Date: 2011-10-20T16:50:11+09:00 Subject: Re: Moving files into alphabetical directories On Thu, Oct 20, 2011 at 12:43 AM, Yossef Mendelssohn wrote: > 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"]} Quite true ! Thanks for the reminder. Jesus.