summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2008-06-23 01:04:10 +0200
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2008-06-23 01:11:36 +0200
commit2897490901dd623fb5b8683971222502ada357dc (patch)
treee12a850eb59b55eef2831bd54af6bea677264f29
parent8a472cf2ea743415829a5f29487928e6e14a45a9 (diff)
extends: define Array#shuffle(\!) only if not present already, and use simpler definitions
-rw-r--r--lib/rbot/core/utils/extends.rb24
1 files changed, 12 insertions, 12 deletions
diff --git a/lib/rbot/core/utils/extends.rb b/lib/rbot/core/utils/extends.rb
index e62e2f21..de6a7db4 100644
--- a/lib/rbot/core/utils/extends.rb
+++ b/lib/rbot/core/utils/extends.rb
@@ -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