Forum: Ruby Add randomize method to array?

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.
2c39e92d3b9c68c7377c4cdb80469f93?d=identicon&s=25 Bart Braem (Guest)
on 2009-01-29 16:57
(Received via mailing list)
Perhaps a silly question. What is the best way to add a randomize
method to array? I know that this code will properly randomize an
array:

a.sort_by{ rand }

but I wonder how I can know add this to the array class. I can't
assign self to the result of this method, so this will not work:

class Array
  def randomize
    self = self.sort_by{ rand }
  end
end

What is the best way to do this?
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-01-29 17:00
(Received via mailing list)
On Fri, 30 Jan 2009, Bart Braem wrote:

>  def randomize
>    self = self.sort_by{ rand }
>  end
> end
>
> What is the best way to do this?

I have no idea how performant this is compared to other ways but the
first thing that occurs to me is:

   class Array
     def randomize
       replace(sort_by { rand })
     end
   end

Ruby 1.9 has shuffle and shuffle! methods to shuffle arrays (the ! one
being an in-place version).


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!
7e593ac63e5f25649b701dc25f69d1b7?d=identicon&s=25 Luis Parravicini (Guest)
on 2009-01-29 17:03
(Received via mailing list)
On Thu, Jan 29, 2009 at 1:57 PM, David A. Black <dblack@rubypal.com>
wrote:
> Ruby 1.9 has shuffle and shuffle! methods to shuffle arrays (the ! one
> being an in-place version).

Ruby 1.8.7 also have shuffle and shuffle!
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-01-29 17:04
(Received via mailing list)
On Fri, 30 Jan 2009, Luis Parravicini wrote:

> On Thu, Jan 29, 2009 at 1:57 PM, David A. Black <dblack@rubypal.com> wrote:
>> Ruby 1.9 has shuffle and shuffle! methods to shuffle arrays (the ! one
>> being an in-place version).
>
> Ruby 1.8.7 also have shuffle and shuffle!

I consider 1.8.7 to be 1.9.-1 :-)


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-01-30 11:50
(Received via mailing list)
2009/1/29 David A. Black <dblack@rubypal.com>:
> I consider 1.8.7 to be 1.9.-1 :-)
Uh, oh this is spooooky...  Reminds me of:

This must thou ken:
Of one make ten,
Pass two, and then
Make square the three,
So rich thou'lt be.
Drop out the four!
From five and six,
Thus says the witch,
Make seven and eight.
So all is straight!
And nine is one,
And ten is none,
This is the witch's one-time-one!

Original

  „Du mußt versteh'n!
   Aus Eins mach Zehn,
   Und Zwei laß geh'n,
   Und Drei mach gleich,
   So bist Du reich.
   Verlier die Vier!
   Aus Fünf und Sechs,
   So sagt die Hex',
   Mach Sieben und Acht,
   So ist's vollbracht:
   Und Neun ist Eins,
   Und Zehn ist keins.
   Das ist das Hexen-Einmaleins!"

http://de.wikipedia.org/wiki/Hexeneinmaleins

Cheers

robert
This topic is locked and can not be replied to.