From: "trans. (T. Onoma)" Date: 2004-09-20T01:58:37+09:00 Subject: Re: Array#index block and rdetect On Sunday 19 September 2004 12:34 pm, David A. Black wrote: > On Sun, 19 Sep 2004, trans. (T. Onoma) wrote: > > Couldn't the index of a hash be considered the key? So the block of > > #index when applied is the value. i.e. > > That's how I tend to see it, though Ruby has two interpretations of > "index" for hashes: > > irb(main):007:0> h = Hash[*%w{a b c d}] > => {"a"=>"b", "c"=>"d"} > irb(main):008:0> h.index("d") > => "c" > irb(main):009:0> h.each_with_index {|e,i| p i if e[0] == "c"} > 1 > > This is one of the reasons I've always felt that Hash#each_with_index > should be eliminated. The "index" in h.each_with_index is not the > same as the "index" of h.index. h.each_with_index is effectively > equivalent to h.to_a.each_with_index, but with the to_a being hidden. Right! Index in this second sense has no significance with regards to Hash proper. To be honest I have always felt a bit odd about Hash being "Enumerable" for this very reason. Hash elements cannot be "numbered" --unless converted (whether by key or by value) into an array first. Perhaps part of the oddness is just that the word "Enumerable" cannotes more then it should. "Indexable" seems more exact. T. -- ( o _ カラチ // trans. / \ transami@runbox.com I don't give a damn for a man that can only spell a word one way. -Mark Twain