From 012f94c76e3571d8e610810899fb2bda5643fc01 Mon Sep 17 00:00:00 2001 From: Jay Thomas Date: Wed, 24 Apr 2013 23:01:56 -0400 Subject: extends: updated shuffle method --- lib/rbot/core/utils/extends.rb | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'lib/rbot') 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 -- cgit v1.2.3