From: "David A. Black" Date: 2008-07-02T00:44:02+09:00 Subject: Re: Hash#select returns an array but Hash#reject returns a hash... --1926193751-41725177-1214927207=:1344 Content-Type: MULTIPART/MIXED; BOUNDARY="1926193751-41725177-1214927207=:1344" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --1926193751-41725177-1214927207=:1344 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Wed, 2 Jul 2008, Rick DeNatale wrote: > On Tue, Jul 1, 2008 at 10:18 AM, David A. Black wrot= e: > >> Hi -- >> >> >> On Tue, 1 Jul 2008, Rick DeNatale wrote: >> >> >>> I'm having a hard time making the connection between this analogy and t= he >>> 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 w= ith >>> 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 on= e > collection to another. They BOTH return a new collection which reference= s > some of the objects referenced by the receiver. It's an analogy, not a roman =C3=A0 clef about some Ruby objects :-) > select returns a collection of all of the objects in the receiver which m= eet > 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 t= he > same species as the receiver on such methods was something I had to get u= sed > to after my Smalltalk experience. > > And, compatibility aside, I think that 1.9 is going in the right directio= n > here. I must not be making it clear that what I'm discussing has nothing to do with whether Object a and Object b are the same object in Ruby's memory, and I'm certainly not arguing that 1.9 should not change what it's changing. I think we're all so used to discussions that are about what should and should not be in the language that it becomes hard to make points that aren't about that kind of advocacy :-) If you have a shelf of books, and you take some off, and then you look back up at your shelf and it has turned into a canvas sack -- that would be kind of weird. Similarly, if you remove items from a hash and the non-removed items get transferred to an array, that would be weird. If, however, you select some books from the shelf, and they end up in a canvas sack (1.8), that isn't necessarily weird. If they end up on another shelf (1.9), that also isn't weird. David --=20 Rails training from David A. Black and Ruby Power and Light: Intro to Ruby on Rails July 21-24 Edison, NJ Advancing With Rails August 18-21 Edison, NJ See http://www.rubypal.com for details and updates! --1926193751-41725177-1214927207=:1344-- --1926193751-41725177-1214927207=:1344--