Posted By

jarnaldich on 04/05/08


array hash

Versions (?)

Who likes this?

1 person have marked this snippet as a favorite


Turning array values into hash keys

 / Published in: Ruby

Useful for speeding up searches.

  1. # a is the array we want to turn into a hash
  2. Hash[*[true] * a.size).flatten]
  4. # Example:
  5. people= %w[John Mary Paul]
  6. query_people=Hash[*[true] * people.size).flatten]
  8. query_people["John"] # returns true
  9. query_people["Wenceslas"] # returns nil (equivalent to false in most tests)

Report this snippet  

You need to login to post a comment.