randomize array content

T

traktorman

Hello,

After 2 months working with ruby i still bump to some of those:

what would be the simplest way (couldn't find a rand method) to
achieve a subject's task?

thanks!
t
 
A

Alex Young

Hello,

After 2 months working with ruby i still bump to some of those:

what would be the simplest way (couldn't find a rand method) to
achieve a subject's task?

If you mean that you want to shuffle the array, then:

array.sort_by{rand}

This comes up often enough that it'd almost be worth adding an
Array#shuffle (and/or #shuffle!) method to core...
 
S

Stefan Rusterholz

Hello,

After 2 months working with ruby i still bump to some of those:

what would be the simplest way (couldn't find a rand method) to
achieve a subject's task?

thanks!
t

ary.sort_by { rand }

Regards
Stefan
 
T

traktorman

If you mean that you want to shuffle the array, then:

array.sort_by{rand}

This comes up often enough that it'd almost be worth adding an
Array#shuffle (and/or #shuffle!) method to core...

Thanks Alex, that would be it!

best
t
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top