Random array element in Ruby
Was looking for the Ruby way to return a random array element.
array = [foo, bar, baz]
puts array[rand(a.size)]
Was looking for the Ruby way to return a random array element.
array = [foo, bar, baz]
puts array[rand(a.size)]
What does this have to do with running?! ;)
(kidding)
I really like messing with arrays in ruby. I like being able to use and - with arrays. I once had a big array of arrays of x, y coordinates (so, like… [[x1, y1], [x2, y2], [x3, y3], …] and I wanted to take out certain ones from the list. I had a list of the ones I didn’t want in the original list anymore, and I could just do “newarray = array1 - array2″. That’s pretty sweet. And =/-= work too, so it could really be “array1 -= array2″