Posted By

jarnaldich on 04/05/08


Tagged

array hash


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

webstic


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[*a.zip([true] * a.size).flatten]
  3.  
  4. # Example:
  5. people= %w[John Mary Paul]
  6. query_people=Hash[*people.zip([true] * people.size).flatten]
  7.  
  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.