]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
extends: pick_one and rand methods for Range
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Mon, 9 Apr 2007 21:17:05 +0000 (21:17 +0000)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Mon, 9 Apr 2007 21:17:05 +0000 (21:17 +0000)
lib/rbot/core/utils/extends.rb

index a749f92adcd64ae7a14a3d466a10ce3a34cde5ae..bcea735b25a77e809c7a1a12fae22ab15421341f 100644 (file)
@@ -27,6 +27,20 @@ class ::Array
   end
 end
 
+# Extensions to the Range class
+#
+class ::Range
+
+  # This method returns a random number between the lower and upper bound
+  #
+  def pick_one
+    len = self.last - self.first
+    len += 1 unless self.exclude_end?
+    self.first + Kernel::rand(len)
+  end
+  alias :rand :pick_one
+end
+
 # Extensions for the Numeric classes
 #
 class ::Numeric