From: Gavin Sinclair Date: 2004-09-21T10:22:14+09:00 Subject: Re: Array#index block and rdetect On Tuesday, September 21, 2004, 1:30:12 AM, Yukihiro wrote: > |Question: if "index" is a basic Enumerable thing, why is there no > |Enumerable#each_index? (My preference would be to push > |#each_with_index down to the individual classes too; but in any case, > |I'm curious about it.) > "index" in "each_with_index" is a loop counter. Perhaps wrong choice > of the word. It happen to be same index in Arrays. I've never cared one way or the other about Hash#each_with_index. It's pretty useless as the numbers tell you nothing meaningful. When I want to iterate a hash completely, I use hash.each do |key, value| ... end and that suits me fine. But even that is rare because of the meaningless output order. So it's usually more like: hash.keys.sort.each do |key| value = hash[key] ... end or hash.sort_by { |key, value| key }.each do |key, value| ... end I don't see a real need for any methods to change. Hash is clearly an enumerable. I see it as a collection of pairs/associations; that's why I use Hash#each instead of the equivalent but redundant Hash#each_pair. Hash#each_with_index is useless, but I don't find it misleading. The very first time I ran 'ri Hash', I was able to work out what's going on. When you see Hash#each_pair and Hash#each_with_index, you know that the "index" doesn't refer to keys. If you defined Hash#each_with_index specially, instead of relying on the Enumerable implementation, I guess this would be sensible: class Hash def each_with_index each_pair do |key, value| yield(value, key) end end end Cheers, Gavin