summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2008-04-09 00:28:18 +0200
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2008-04-09 01:30:42 +0200
commit3220c8e65e723790dfd69e05c012ad78badfbb45 (patch)
tree8c9f183e159024a388f822544e760c355dbf2dd0
parentcfc6d31ac24b3c705eaa7302ced01d5f33135dc4 (diff)
extends: Array#shuffle! and shuffle methods
-rw-r--r--lib/rbot/core/utils/extends.rb18
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/rbot/core/utils/extends.rb b/lib/rbot/core/utils/extends.rb
index f3861745..19157009 100644
--- a/lib/rbot/core/utils/extends.rb
+++ b/lib/rbot/core/utils/extends.rb
@@ -109,6 +109,24 @@ class ::Array
end
self.delete_at(index)
end
+
+ # This method shuffles the items in the array
+ def shuffle!
+ base = self.dup
+ self.clear
+ while item = base.delete_one
+ self << item
+ end
+ self
+ end
+
+ # This method returns a new array with the same items as
+ # the receiver, but shuffled
+ def shuffle
+ ret = self.dup
+ ret.shuffle!
+ end
+
end
# Extensions to the Range class