]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/core/utils/extends.rb
config core botmodule: only show possible keys summary when more than one key was...
[user/henk/code/ruby/rbot.git] / lib / rbot / core / utils / extends.rb
index e9581bc0902a466039ce443003a31c0b7199d957..e62e2f21ea768ec846a7b607020ecaf6c4d9cf73 100644 (file)
@@ -4,8 +4,6 @@
 # :title: Standard classes extensions
 #
 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
-# Copyright:: (C) 2006,2007 Giuseppe Bilotta
-# License:: GPL v2
 #
 # This file collects extensions to standard Ruby classes and to some core rbot
 # classes to be used by the various plugins
@@ -94,13 +92,39 @@ class ::Array
     self[rand(self.length)]
   end
 
-  # This method returns a random element from the array, deleting it from the
-  # array itself. The method returns nil if the array is empty
+  # This method returns a given element from the array, deleting it from the
+  # array itself. The method returns nil if the element couldn't be found.
   #
-  def delete_one
+  # If nil is specified, a random element is returned and deleted.
+  #
+  def delete_one(val=nil)
     return nil if self.empty?
-    self.delete_at(rand(self.length))
+    if val.nil?
+      index = rand(self.length)
+    else
+      index = self.index(val)
+      return nil unless index
+    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