Guest User

Untitled

a guest
Dec 16th, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.37 KB | None | 0 0
  1. keys=['hal',666,[1,2,3]]
  2. vals=['ibm','devil',123]
  3.  
  4. if RUBY_VERSION >= '1.8.7'
  5.   # Easy way, but needs Ruby 1.8.7 or later.
  6.   hash = Hash[keys.zip(vals)]
  7. else
  8.   hash = keys.zip(vals).inject({}) {|h, kv| h.store(*kv); h }
  9. end
  10.  
  11. p hash  # => {"hal"=>"ibm", 666=>"devil", [1, 2, 3]=>123}
  12.  
  13. #retrieve the value linked to the key [1,2,3]
  14. puts hash[ [1,2,3] ]  # => 123
Advertisement
Add Comment
Please, Sign In to add comment