From: David Flanagan Date: 2007-09-07T07:11:52+09:00 Subject: New array methods cycle, choice, shuffle (plus bug in cycle) Four new methods have been added to Array the Ruby 1.9 trunk. I've got a couple of questions and comments and one bug report: cycle is an iterator that is like calling each on the array within an infinite loop. Matz: have you considered generalizing this to be a method of Enumerable? cycle has what I'd consider a bug: call it on an empty array and it will never return and will never invoke the associated block. ruby1.9 -e '[].cycle {}' When I do this I can't even ^C to stop ruby. I have to kill -9 the process. I'd suggest that cycle simply return when the array is empty. That is, it cycles until the array becomes empty (which will be forever in most cases). The patch I propose is at the bottom of this message. choice returns a random element from the array. I don't like the name of this method: neither the word "choice" nor it verb form "choose" imply randomness--instead they suggest the opposite. I suggest just using the name "rand", "random" or "random_element". Most array methods are verbs, but a noun here would match the existing methods first and last. Also, may I suggest a random! variant of the method that returns *and removes* a random element from the array? first and last already have shift and pop to serve this purpose. shuffle and shuffle! randomly permute an array and seem worth having. I think I remember Josh Bloch (author of the Java collections library) writing somewhere that shuffling an array correctly is actually kind of hard to do, so I think it is great to have these methods, even if I can't think of a use case right now :-) David --- array.c.orig 2007-09-06 15:00:02.000000000 -0700 +++ array.c 2007-09-06 15:03:30.000000000 -0700 @@ -2940,6 +2940,7 @@ RETURN_ENUMERATOR(ary, 0, 0); for (;;) { + if (RARRAY_LEN(ary) == 0) return Qnil; for (i=0; i