Permalink
Browse files

added randomize and randomize! to the array class so we can randomize…

… the participant order in teasing mode. Also reduced the sleep time in teasing mode.
  • Loading branch information...
1 parent e7045e0 commit 748a5e97ee62525cabe31d5bf8d7089999fae34a @mattetti committed Apr 4, 2008
Showing with 32 additions and 3 deletions.
  1. +1 −2 README
  2. +24 −1 lib/raffle.rb
  3. +7 −0 spec/raffle_spec.rb
View
3 README
@@ -5,7 +5,6 @@ Usage:
------
require 'lib/raffle'
-raffle = Raffle.new(%W{Patrick Ryan Jordan Tom Chris Rob Sam Matt})
>> raffle.participants
=> ["Patrick", "Ryan", "Jordan", "Tom", "Chris", "Rob", "Sam", "Matt"]
@@ -18,4 +17,4 @@ raffle = Raffle.new(%W{Patrick Ryan Jordan Tom Chris Rob Sam Matt})
(note that tom has been removed from the participants list)
-use #teasing_mode to go though the entire list of loosers before giving away the winner's name
+use #teasing_mode to go though the entire list of losers before giving away the winner's name
View
@@ -1,6 +1,28 @@
$:.unshift File.dirname(__FILE__)
require 'raffle/version'
+class Array
+
+ def randomize
+ a = self.dup
+ result = []
+ self.length.times do
+ result << a.slice!(rand(a.length))
+ end
+ return result
+ end
+
+ def randomize!
+ a = self.dup
+ result = []
+ self.length.times do
+ result << a.slice!(rand(a.length))
+ end
+ self.replace result
+ end
+
+end
+
class Raffle
include RaffleInfo
@@ -31,14 +53,15 @@ def teasing_mode
def tease_the_loosers(winner)
p "...and the winner is..."
+ @participants.randomize!
@participants.each do |looser|
looser(looser)
end
p "... #{winner} ... YAY!"
end
def looser(name)
- sleep 1
+ sleep 0.5
p "... #{name} ... NOT"
end
View
@@ -46,4 +46,11 @@ def create_raffle
@raffle.mode.should == 'teasing'
end
+end
+
+describe 'randomize' do
+
+ ['test1', 'test2', 'test3', 'test4'].randomize.should_not == ['test1', 'test2', 'test3', 'test4']
+ ['test1', 'test2', 'test3', 'test4'].randomize.sort.should == ['test1', 'test2', 'test3', 'test4']
+
end

0 comments on commit 748a5e9

Please sign in to comment.