From: Rick DeNatale Date: 2008-07-02T00:28:53+09:00 Subject: Re: Hash#select returns an array but Hash#reject returns a hash... ------=_Part_4852_13288200.1214926306523 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Tue, Jul 1, 2008 at 10:18 AM, David A. Black wrote: > Hi -- > > > On Tue, 1 Jul 2008, Rick DeNatale wrote: > > >> I'm having a hard time making the connection between this analogy and the >> methods. >> >> In the first case one could say that we end up with two lines, the >> 'original' one and the line of rejects. >> >> But first of all, x.reject leaves x alone so the original 'line' is >> unchanged. >> >> And why can't we see your select example exactly the same way, except with >> the resultant line of 'selects' just closer to you. >> >> > It's specifically not about the exact objects, though. As I said, > "same line" in this model doesn't map to "same object". Rather, the > analogy has to do with types of structure or container. > > Here's another, perhaps better analogy. You've got a shelf of books, > and you've got an empty box. You select some books from the shelf, and > put them in the box. Now you've got two structures: a shelf, and a > box. > > If you were describing this operation, you'd say: "I selected some > books and put them in a box." The box of books is the result of the > operation, and is structurally different from the shelf. The point is > not that this is the only way it can be done, but that it *can* be > done this way. It's reasonable to select from a collection into > something that is not only a different collection, but a different > container type. > > Now, if you do it reject-wise, you take the books you don't want and > throw them out the window. The box never enters into it. You're not > selecting, so you don't care about observing or measuring the books > that you're removing from the shelf. > I still have the same problem with this analogy. reject and select are not about the books. They don't move books from one collection to another. They BOTH return a new collection which references some of the objects referenced by the receiver. select returns a collection of all of the objects in the receiver which meet the criterion specified by the block, reject returns a collection of all the objects in the receiver which DON'T meet the criterion specified by the block. The fact that Ruby <1.9 tends to return arrays rather than something of the same species as the receiver on such methods was something I had to get used to after my Smalltalk experience. And, compatibility aside, I think that 1.9 is going in the right direction here. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ ------=_Part_4852_13288200.1214926306523--