From: dblack@... Date: 2007-06-25T22:47:38+09:00 Subject: Re: Behaviour of Enumerables reject vs. select mixed into Hash Hi -- On Fri, 22 Jun 2007, Trans wrote: > > >> On 6/21/07, Rob Biedenharn wrote: >> >>> What no one seems to have (directly) focussed on is that Hash#each >>> gives two-element [k,v] arrays back as the content. > > That's right. And if you wanted things to be "just ducky", it would > have to give only the v. I've actually argued in favor of that before, > b/c it's not unreasonable to see that an Array index is like a Hash > key. So, for a full parallel we'd need to see something like: > > {:x=>'m'}.each { |v| v #=> 'm' > > ['m'].each { |v| v #=> 'm' > > {'x'=>'m'}.each_assoc{ |a| a #=> ['x','m'] > > ['m'].each_assoc{ |a| a #=> [0,'m'] > > One could easily argue that an Assoc class would be quite useful here, > rather than relying on 2-element Array to fulfill the roll. With that > in hand, it would re easy enough to add the #<< method for enumerable > construction. I don't think there's any reason to expect just the value when iterating through a hash. In fact if you're using a hash, you probably have a reason for storing data that way and iterating pair-wise seems logical to me. I think it's a question of how one looks at the underlying types or behaviors. I don't think the language has to converge around the smallest possible number of interfaces. You can look at it the other way around. It's useful (extremely) to have hashes, and to iterate over them in pairs. Enumerable is one way to help introduce that construct into the language -- not a one-stop-shopping hash implementation, but helpful. If we then find fault with hash behavior because it's not in line precisely with other enumerables, that's a kind of reverse logic; it's a way to talk the language out of having something useful, which I don't think is a good idea. Enumerable not only allows but requires that each class implement #each, and there's no constraint that every enumerable class has to yield exactly one value at a time. I'd want to see more concrete evidence that having hashes and arrays behave differently is really creating problems before wanting to normalize them around one construct. David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)