From: "David A. Black" Date: 2004-09-21T20:09:35+09:00 Subject: Re: Array#index block and rdetect Hi -- On Tue, 21 Sep 2004, Yukihiro Matsumoto wrote: > Hi, > > In message "Re: Array#index block and rdetect" > on Tue, 21 Sep 2004 10:56:51 +0900, "David A. Black" writes: > > |Here's one possibility. I don't think this would be bad for > |compatibility, because Array would have its *_index methods, and I > |don't think #each_with_index is used much (ever?) for anything except > |arrays and maybe array subclasses. > | > | Enumerable: > | > | each_with_counter # "dumb" counters, for every enumerable, > | each_counter # strictly based on incrementing through > | # #each > > No need for each_counter, for there's no way for general enumerables > to map from counters to values. If it's possible, it's rather > suitable to be called as index. By the way, do you agree with the > word 'counter'? It's kinda burden for me, non English speaker, to > tell the word nuance. I think it's OK. "counter" is very similar to an expression like: "maintaining a counter in a loop". It also has a "dumb" sound (in a good way :-) that keeps it separate from index and key. (Index and key both suggest a direct lookup, whereas counter suggests an external numbering which could apply to a non-lookup enumerable as well as a lookup enumerable.) > | Array: > | > | each_with_index # either very similar to, or identical to, > | each_index # the *_counter methods (because that > | # happens to be how arrays work). > > No objection here. > > | Hash and other enumerable classes: > | > | In addition to the *_counter methods, each enumerable class > | can have its own, "smart" methods (just as Array does with > | its *_index methods). They can have "index" in the name, > | if appropriate -- but they do not have to. (There are no > | Enumerable#*_index methods.) > > Do you have any opinion toward Hash#each_with_index? I prefer "key" for hashes and "index" for arrays (I know Robert disagrees), so I would tend to avoid "index" names for hash methods. But I have no deep theoretical argument here. I'm just following what I think is common usage (index/element for array, key/value for hash). David -- David A. Black dblack@wobblini.net