From: "David A. Black" Date: 2008-08-08T05:45:19+09:00 Subject: [ruby-core:18176] Re: New array methods cycle, choice, shuffle (plus bug in cycle) Hi -- On Fri, 8 Aug 2008, David Flanagan wrote: > David A. Black wrote: > >> I also agree with something I think you said before, which is that >> sample! as described here seems to be doing two things. Usually the >> "danger" of the bang fits very well with the receiver-changing >> behavior of arrays (plus the secondary danger of the nil return). But >> it's a stretch, I think, to think of a sampling as a removal >> operation. It feels a bit like having first! remove the first element. > > I don't really get the objection to using a bang in the name, though. While > its not strictly parallel, I can't see it confusing anyone. I'm replying to > David's comment about this, though, because when I was trying to put together > an argument *for* sample!, I was going to use a hypothetical first! as an > example: it seems to me that anyone who was familiar with Ruby method naming > conventions would understand that first!, if it existed, worked just like > shift. first! and last! would certainly be easier for new programmers who > haven't already been introduced to terms like shift and pop. (I'm not asking > to add these methods--just saying that they seem perfectly reasonable to me!) I don't see first and last as being just a ! away from being destructive. I wouldn't expect there to be a destructive at! method either. I think it has something to do with the fact that these methods don't do anything to the array in the first place, so the leap to doing that (nothing) destructively seems too great. By contrast, all of the existing array bang methods (sort!, reverse!, collect!, compact!, reject!, map!, slice!, flatten!, uniq!) all perform transformations on the array, with the possible except of slice which sounds kind of destructive already. Going from, say, reversing a dup to reversing the object itself seems like a smaller and smoother step than going from referring to an element to removing that element permanently. Come to think of it, they're all verbs, while first, last, and at aren't. That may be the heart of it. David -- Rails training from David A. Black and Ruby Power and Light: * Advancing With Rails August 18-21 Edison, NJ * Co-taught by D.A. Black and Erik Kastner See http://www.rubypal.com for details and updates!