From: Mark Hubbart Date: 2005-10-08T06:33:55+09:00 Subject: Re: select! not present but reject! is On 10/7/05, Jacob Fugal wrote: > On 10/7/05, David A. Black wrote: > > Hi -- > > > > On Fri, 7 Oct 2005, Rob Rypka wrote: > > > > > By your logic, reject! is also wrong. johnny.reject! should reject > > > the items, and then give them to you, which is the same as your > > > select!. That's not what it does, though... > > > > See my previous post -- it shows the difference (between reject! and > > my sense of select!), which is in the return values. > > And that's where it confuses me. select and reject are complements, so > why couldn't it be expected that select! and reject! be complements as > well? Changing the semantics of select from converse of reject without > bang to synonym for reject (differing only in return value) with bang > makes no sense at all to me. > > > > In all honesty, I don't care what it's called, but reject! feels like > > > it should have an opposite. > > > > Similarly (in case it's unclear), I have no underlying objection to > > the idea of a reject! opposite :-) > > At least we're all agreed on that. :) > > I just fail to see why select should lose it's complementary > relationship with reject when a bang is added. I suggest we retain > that relationship and let select! and reject! remain complementary. > With select and reject we have the following tautology[1]: > > def test(ary) > selected = ary.select { |e| yield e } > rejected = ary.reject { |e| yield e } > rebuilt = selected + rejected > (rebuilt - ary).empty? and (ary - rebuilt).empty? > end > > I propose select! and reject! should have a similar tautology: > > def test(ary) > selected = ary.dup.select! { |e| yield e } > rejected = ary.dup.reject! { |e| yield e } > rebuilt = selected + rejected > (rebuilt - ary).empty? and (ary - rebuilt).empty? > end The english words "select" and "reject" are not opposites. "reject" and "keep" might be, as someone else suggested. Going back, say you have a box of apples, some of which are shiny, the rest of which are dull. You don't want the dull ones. If you say: apple_box.reject! {|apple| apple.dull?} ...it sounds like you want to reject, or throw out, any dull apples, leaving only the shiny ones. The opposite would be: apple_box.keep! {|apple| apple.shiny?} ...because you want to keep only the shiny ones. "keep" implies that the rest are not kept, and are thrown out. On the other hand, if you said: apple_box.select! {|apple| apple.shiny?} It implies that you want to keep both; you remove the selected ones from the box, and leave the unselected ones behind. Note that your tautology (as written) for select!/reject! works for this version also. "select!" would return an array of the values removed from the apple_box. Also, I'm not really advocating any action here. I'm just suggesting that it would be a little weird for "select!" to be the exact opposite of "reject!", since the Ruby definitions wouldn't stand up to the English ones. cheers, Mark