Posted By

Shamaoke on 02/26/11


Tagged

ruby index inject


Versions (?)

inject with index


 / Published in: Ruby
 

  1. # encoding: utf-8
  2.  
  3. ['one', 'two', 'three']
  4. .each_with_index
  5. .inject({}) { |acc, (elm, i)|
  6. acc.merge Hash[i.succ, elm]
  7. }
  8.  
  9. # {1 => 'one', 2 => 'two', 3 => 'three'}

Report this snippet  

You need to login to post a comment.