]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
extends: define Array#shuffle(\!) only if not present already, and use simpler defini...
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sun, 22 Jun 2008 23:04:10 +0000 (01:04 +0200)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sun, 22 Jun 2008 23:11:36 +0000 (01:11 +0200)
lib/rbot/core/utils/extends.rb

index e62e2f21ea768ec846a7b607020ecaf6c4d9cf73..de6a7db4f1cbfe7743be516db68629c2b1619d82 100644 (file)
@@ -108,21 +108,21 @@ class ::Array
     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
+  # 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
-  def shuffle
-    ret = self.dup
-    ret.shuffle!
+  unless method_defined? :shuffle
+    def shuffle
+      sort_by { rand }
+    end
+  end
+
+  # This method shuffles the items in the array
+  unless method_defined? :shuffle!
+    def shuffle!
+      replace shuffle
+    end
   end
 
 end