ruby doc
method
http://apidock测试数据/ruby/Enumerable/each_with_index
each_with_index
Ruby latest stable (v1_8_7_72) - 0 notes - Class: Enumerable
1_8_6_287 (0) 1_8_7_72 (-38) What's this?
Related methods Instance methods (45) all? any? collect count cycle detect drop drop_while each_cons each_slice each_with_index entries enum_cons enum_slice enum_with_index find find_all find_index first grep group_by include? inject map max max_by member? min min_by minmax minmax_by none? one? partition reduce reject reverse_each select sort sort_by take take_while to_a to_set zip
= private
= protected
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测试数据/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测试数据/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>
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did43659