From: William Morgan Date: 2005-07-11T02:37:57+09:00 Subject: Re: accessing index inside map (*_with_index) Excerpts from daz's mail of 8 Jul 2005 (EDT): > One major factor that has bugged me is the lack of symmetry > between #each and #e_w_i which discourages temporary changes. > > h = {:a => 1, :b => 2, :c => 3} > # Current conversion of Hash#each to #each_with_index > > h.each {|ek, ev| p [ek , ev ]} > h.each_with_index {|e, idx| p [e[0], e[1], idx]} > # ^_________^ ^_^ ^_^ ^^_^ ^_^ > # A B C D C E > > # A. Change the method name to a much longer one. > # B. Name the index variable. > # C. Hash#each gives key and value -- #each_with_index > # passes these as a two-element array into the first > # block parameter. Specify which element. > # D. The name you chose for the second block parameter of each > # will, probably, need changing (if you used, say, 'key, val'). > # E. Use the index variable. Minor point, but you can also do h.each_with_index { |(ek, ev), idx| p [ek, ev, idx] } which solves C and D (though I guess introduces an "F"). -- William