Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

  "{(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


Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: