summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2007-04-06 10:56:01 +0000
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2007-04-06 10:56:01 +0000
commit2c65a50c9fbdbdaab20d76b73e9a68023739417f (patch)
treeb129bba9cccc4e0057b20f42670464cd07d212e2 /lib
parent809d3eb28fe8176043696483b26f1ddbcf341cf6 (diff)
Extend Numeric class with a clip() method to force a number to be in a given range
Diffstat (limited to 'lib')
-rw-r--r--lib/rbot/core/utils/extends.rb18
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/rbot/core/utils/extends.rb b/lib/rbot/core/utils/extends.rb
index 4b7e57f3..b812a676 100644
--- a/lib/rbot/core/utils/extends.rb
+++ b/lib/rbot/core/utils/extends.rb
@@ -27,6 +27,24 @@ class ::Array
end
end
+# Extensions for the Numeric classes
+#
+class ::Numeric
+
+ # This method forces a real number to be not more than a given positive
+ # number or not less than a given positive number, or between two any given
+ # numbers
+ #
+ def clip(left,right=0)
+ raise ArgumentError unless left.kind_of?(Numeric) and right.kind_of?(Numeric)
+ l = [left,right].min
+ u = [left,right].max
+ return l if self < l
+ return u if self > u
+ return self
+ end
+end
+
# Extensions to the String class
#
# TODO make ircify_html() accept an Hash of options, and make riphtml() just