method
http://apidock.com/ruby/Enumerable/each_with_index
http://apidock.com/ruby/Enumerable/each_with_index
each_with_index
Ruby latest stable (v1_8_7_72) - 0 notes - Class: Enumerable
each_with_index()
public Calls block with two arguments, the item and its index, for each item in enum.
<span class="ident">hash</span> <span class="punct">=</span> <span class="constant"><a href="http://apidock.com/ruby/Hash">Hash</a></span><span class="punct">.</span><span class="ident">new</span> <span class="punct">%w(</span><span class="string">cat dog wombat</span><span class="punct">).</span><span class="ident"><a href="http://apidock.com/ruby/Enumerable/each_with_index">each_with_index</a></span> <span class="punct">{|</span><span class="ident">item</span><span class="punct">,</span> <span class="ident">index</span><span class="punct">|</span> <span class="ident">hash</span><span class="punct">[</span><span class="ident">item</span><span class="punct">]</span> <span class="punct">=</span> <span class="ident">index</span> <span class="punct">}</span> <span class="ident">hash</span> <span class="comment">#=> {"cat"=>0, "wombat"=>2, "dog"=>1}</span>