> Hi. Can anyone help with this? I'd like to end with a hash like so:
>=A0
> hash =3D {{"A" =3D> 'alpha'}=2C {'B' =3D> 'bravo'}} etc...
>=A0
>=A0
>=A0
> I'm not sure whether I can use group_by and how to handle case
> insensitity. Cheers for any help.
>=A0
>=A0
>=A0
> irb(main):001:0> s =3D ('A'..'Z').to_a
> =3D> ["A"=2C "B"=2C "C"=2C "D"=2C "E"=2C "F"=2C "G"=2C "H"=2C "I"=2C "J"=
=2C "K"=2C "L"=2C "M"=2C
> "N"=2C "O"=2C "P"=2C "Q"=2C "R"=2C "S"=2C "T"=2C "U"=2C "V"=2C "W"=2C "X"=
=2C "Y"=2C "Z"]
>=A0
> irb(main):003:0> t =3D %w(alpha bravo Charlie Zebra)
> =3D> ["alpha"=2C "bravo"=2C "Charlie"=2C "Zebra"]
I will suggest something different based on what you wish to do. This uses =
inject:
---t =3D %w(alpha bravo Charlie Zebra)
result =3D t.inject(Hash.new) do | words=2C word |=A0 first_letter =3D word=
[0].upcase=A0 words[first_letter] =3D word=A0 wordsend---
result looks like this:
{"A"=3D>"alpha"=2C "B"=3D>"bravo"=2C "C"=3D>"Charlie"=2C "Z"=3D>"Zebra"}=A0
Now=2C if there's a chance that words with start with the letter A=2C or an=
y other letter might occur=2C I instead recommend an array based approach:
---t =3D %w(alpha bravo Charlie Zebra)
result =3D t.inject(Hash.new) do | words=2Cword |=A0 first_letter =3D word[=
0].upcase=A0 if words.has_key? first_letter=A0 =A0 words[first_letter] << w=
ord=A0 else=A0 =A0 words[first_letter] =3D [word]=A0 end=A0 wordsend---
result produces:
{"A"=3D>["alpha"]=2C "B"=3D>["bravo"]=2C "C"=3D>["Charlie"]=2C "Z"=3D>["Zeb=
ra"]} =A0
In both cases=2C the first letter is uppercased to handle being case insens=
itive. Let me know if you have any questions regarding this=2C or if you ha=
ve a different problem you are trying to solve.
Best Regards=2CChris White 		 	   		  =