[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Add randomize method to array?

Bart Braem

1/29/2009 3:51:00 PM

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?
4 Answers

David A. Black

1/29/2009 3:57:00 PM

0

On Fri, 30 Jan 2009, Bart Braem wrote:

> 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?

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.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

http://www.wis... => Independent, social wishlist management!

Luis Parravicini

1/29/2009 4:01:00 PM

0

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!


--
Luis Parravicini
http://ktulu.co...

David A. Black

1/29/2009 4:02:00 PM

0

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.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

http://www.wis... => Independent, social wishlist management!

Robert Klemme

1/30/2009 10:47:00 AM

0

2009/1/29 David A. Black <dblack@rubypal.com>:
> 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 :-)

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

=84Du mu=DFt versteh'n!
Aus Eins mach Zehn,
Und Zwei la=DF geh'n,
Und Drei mach gleich,
So bist Du reich.
Verlier die Vier!
Aus F=FCnf 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/Hexen...

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end