inject with index


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.