4 ms·
"{(x, random.randrange(100)) for x in ["x", "y", "z"]}" FWIW, it's possible to do it as a one liner in Ruby 1.9 without tap: Hash[[:x, :y, :z].map { |l| [l,
by carbon8 17y ago
"{(x, random.randrange(100)) for x in ["x", "y", "z"]}"
FWIW, it's possible to do it as a one liner in Ruby 1.9 without tap:
Hash[[:x, :y, :z].map { |l| [l, rand(100)]}]
There may be other ways to do it (like inject, as mentioned in the post). That said, tap is probably more confusing and not necessarily shorter than:
result = {}
[:x, :y, :z].map { |l| result[l] = rand(100)}
result