[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

randomize array content

traktorman@gmail.com

5/28/2007 5:55:00 PM

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

3 Answers

Alex Young

5/28/2007 6:08:00 PM

0

traktorman@gmail.com wrote:
> 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...

--
Alex

Stefan Rusterholz

5/28/2007 6:08:00 PM

0

traktorman@gmail.com wrote:
> 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

--
Posted via http://www.ruby-....

traktorman@gmail.com

5/28/2007 6:49:00 PM

0

On May 28, 2:07 pm, Alex Young <a...@blackkettle.org> wrote:
> traktor...@gmail.com wrote:
> > 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...
>
> --
> Alex

Thanks Alex, that would be it!

best
t