5 Answers
5

This is essentially two steps (rand(array.length) and array.delete_at(idx)). OP did ask for only one method, but probably one of the simplest ways to perform this kind of operation in Ruby.
–
sindrenmAug 8 '13 at 15:38

Bottom line: something like your second example (although in the language in which the interpreter is implemented) should be part of the core Array implementation. I would probably call it something like delete_sample! or something, though, as I find it a bit more obvious what it does. :-)
–
sindrenmAug 29 '13 at 7:38

If you go for this option, and you want to remove the item from original array you better do array.shuffle!.pop, or else your array will remain as it was. Doing shuffle without bang (!) will just give you another array to pop from.
–
mjnissimJan 30 at 9:33

This one may be less efficient than the others (delete requires a search) but it's obvious at a glance what it's doing.
–
micapamAug 28 '13 at 5:17

This is true, but I think it's a bit more readable than using rand, as there already is a way to find a random element in an array. Also, we don't have to drag in the length of the array, which is something we really shouldn't need to care about. :-)
–
sindrenmAug 29 '13 at 7:34

This approach runs into problems when you have duplicates in an array because delete removes all by equality. For instance in this example, if sample gets the "a". ["a","a","b"].delete sample ==> deleted: "a", remaining: ["b"]
–
BookOfGregMar 26 at 0:22