Forum: Ruby randomize array content

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6cb6f977482f14508080300b05f2946a?d=identicon&s=25 traktorman@gmail.com (Guest)
on 2007-05-28 20:00
(Received via mailing list)
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
Cf7cd97cdc8ed7d4ae92965b24f0dfad?d=identicon&s=25 Stefan Rusterholz (apeiros)
on 2007-05-28 20:08
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
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (regularfry)
on 2007-05-28 20:09
(Received via mailing list)
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...
6cb6f977482f14508080300b05f2946a?d=identicon&s=25 traktorman@gmail.com (Guest)
on 2007-05-28 20:51
(Received via mailing list)
On May 28, 2:07 pm, Alex Young <a...@blackkettle.org> wrote:
> 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
This topic is locked and can not be replied to.