From: 7rans Date: 2009-08-29T21:32:21+09:00 Subject: Re: #map, #select semantics On Aug 29, 5:50 am, "David A. Black" wrote: > I don't mean that *something* can't be defined for every class; I mean > specifically that it will never be the case that every Enumerable > class will return an instance of itself from its iterations. That's true. And we would never really want it to. There are many classes we may make Enumerable just to have the conveniences for some internal property. For example, I once defined an #each method for ObjectSpace, and made it enumerable. I sure wouldn't want a new ObjectSpace returned! ;-) I think people who suggest the idea, don't really mean exactly what they think they do. That is to say, they see Set and Hash enumerable methods returning Arrays and think, why not Sets and Hashes? Then they take the mental leap to the idea that enumerables ought to return an instance of the originating class. But on further reflection, they would realize that the ideal solution is a predictable and controllable system for determining the return container. It's a pretty powerful idea. It could even be designed to allow the container to be changed on the fly. Let's say we have [[:a,1], [:b,2]].map{ |(k,v)| [k, v+1] } #=> [[:a,2], [:b,3]] We might have something like: [[:a,1], [:b,2]].using({}).map{ |(k,v)| [k, v+1] } #=> {:a=>2, :b=>3} --