]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
extends: Array#shuffle! and shuffle methods
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Tue, 8 Apr 2008 22:28:18 +0000 (00:28 +0200)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Tue, 8 Apr 2008 23:30:42 +0000 (01:30 +0200)
lib/rbot/core/utils/extends.rb

index f3861745442035ebb60e926b61188ec3c0d5aec8..19157009a06b9f9d264f1c08584844421155fc1d 100644 (file)
@@ -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