From: David Vallner Date: 2006-10-29T22:12:54+09:00 Subject: Re: Define a hash using %q? --------------enig02D796132252D971CE39A3D5 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Robert Klemme wrote: > Logan Capaldo wrote: >> Alternative decandence: >> >> class Array >> def to_hsh >> require 'enumerator' >> to_enum(:each_slice, 2).to_a.inject({}) { |h, (k, v)| >> h.update(k=3D>v) } >> end >> end >=20 > This seems a bit inefficient. If you write a method then I'd prefer >=20 > require 'enumerator' > module Enumerable > def to_hash > h =3D {} > to_enum(:each_slice, 2).each {|k,v| h[k]=3Dv} > h > end > end >=20 > irb(main):021:0> %w( a b c d ).to_hash > =3D> {"a"=3D>"b", "c"=3D>"d"} > irb(main):022:0> (1..10).to_hash > =3D> {5=3D>6, 1=3D>2, 7=3D>8, 3=3D>4, 9=3D>10} >=20 Well, if Ruby had clean blocks, the method using #inject might be more efficient - I don't know how expensive the implicit hash construction for method parameters is. Smalltalk muscle memory working there? David Vallner --------------enig02D796132252D971CE39A3D5 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (MingW32) iD8DBQFFRKjHy6MhrS8astoRAnWBAJsF0UsTiZjrDrIBoXslOB9jGfB6RgCdEoud goAoKw2teM1P93eMds+E6+I= =0ZOV -----END PGP SIGNATURE----- --------------enig02D796132252D971CE39A3D5--