From: "David A. Black" Date: 2005-10-07T10:10:41+09:00 Subject: Re: select! not present but reject! is Hi -- On Fri, 7 Oct 2005, Jacob Fugal wrote: > On 10/6/05, David A. Black wrote: >> ... >> >> In the middle of writing this I finally got a handle on what bothers >> me about select! as (I think) it's being discussed: it's backwards. >> >> If you select! elements from an array (dangerous/destructive select), >> those elements should be *removed* from the array. It's like >> selecting people from the audience to come onto the stage: they are >> removed from the audience. The audience is not reduced to being just >> them. > > Hmm, I'd disagree. After all, we're sending the message to the array > aren't we? That's true across the board, though; it's not a determinant of what happens when we send a message. > I look at it like John the Innocent Array has five apples, > two shiny and three dull. I come along, as the wise programmer, and > say to Johnny, "Johnny, shiny apples are good. You only need to keep > those ones." In Ruby: > > johnny = [] > 2.times { johnny << ShinyApple.new } > 3.times { johnny << DullApple.new } > > # ... later ... > > johnny.select! { |a| a.shiny? } Or you could look at it as: "Give me (permanently) all your shiny apples!" my_apples = johnny.select! {|a| a.shiny? } So now my_apples has them, and johnny doesn't. Mind you, I'm not eager to see my version in the language either. I don't like the bang method not returning the receiver. But I don't like the semantics, in this case, when it does. (And the find_all! thing is a further sign that this method was not meant to have a bang version.) I think there's been talk in the past of delete_unless. delete_if is not quite the same as reject! (one of them returns nil if nothing's found; the other returns an array, I believe). David -- David A. Black dblack@wobblini.net