summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJay Thomas <degradinglight@gmail.com>2013-04-24 23:01:56 -0400
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2013-04-25 06:44:18 +0200
commit012f94c76e3571d8e610810899fb2bda5643fc01 (patch)
tree3a2fbfccaa10a5ed372cc38258b781a4bc6772a7 /lib
parent76308815c268c6f7d13a31594da8b9709d17564b (diff)
extends: updated shuffle method
Diffstat (limited to 'lib')
-rw-r--r--lib/rbot/core/utils/extends.rb24
1 files changed, 12 insertions, 12 deletions
diff --git a/lib/rbot/core/utils/extends.rb b/lib/rbot/core/utils/extends.rb
index 208cb089..f3662395 100644
--- a/lib/rbot/core/utils/extends.rb
+++ b/lib/rbot/core/utils/extends.rb
@@ -119,22 +119,22 @@ class ::Array
end
end
+ # Taken from Ruby backports:
# shuffle and shuffle! are defined in Ruby >= 1.8.7
- # This method returns a new array with the same items as
- # the receiver, but shuffled
- unless method_defined? :shuffle
- def shuffle
- sort_by { rand }
- end
- end
+ # This method returns a new array with the
+ # same items as the receiver, but shuffled
+ def shuffle
+ dup.shuffle!
+ end unless method_defined? :shuffle
- # This method shuffles the items in the array
- unless method_defined? :shuffle!
- def shuffle!
- replace shuffle
+ def shuffle!
+ size.times do |i|
+ r = i + Kernel.rand(size - i)
+ self[i], self[r] = self[r], self[i]
end
- end
+ self
+ end unless method_defined? :shuffle!
end
module ::Enumerable