From: John Carter Date: 2005-05-04T07:29:05+09:00 Subject: Re: mapping an array to a hash? On Wed, 4 May 2005, Nick Woolley wrote: > Can anyone suggest an idiomatic ruby equivalent to this perl snippet? > > my %index = map { $_ => compute_something($_) } @files; > The best I came up with so far seems reletively clunky to me: > > index = Hash.new() > files.each {|f| index[f] = compute_something(f) } If you want mind bending and obscure, try... index = Hash.new{|hash,key| hash[key] = compute_something(key)} and then somewhere totally elsewhen in the code... files.each{|f| index[f]} John Carter Phone : (64)(3) 358 6639 Tait Electronics Fax : (64)(3) 359 4632 PO Box 1645 Christchurch Email : john.carter@tait.co.nz New Zealand Somewhere on the edge of a Galaxy, one of literally billions of such galaxies, is a sun, one of literally billions of suns in that galaxy. Orbiting that sun is a small rock 330000 times smaller than that sun. This rock is covered by a very very thin scum of life. (Think 6000km of rock followed by a meter or so of biomass.) Amongst the millions of species in that scum are many hundreds of thousands of types beetle and a mere handful of primates. Surprisingly enough, this email does not originate from a beetle. It originates from just one of the 6 billion vastly outnumbered humans. I trust you will keep this perspective and context in mind when reacting to this email.