From 2c65a50c9fbdbdaab20d76b73e9a68023739417f Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Fri, 6 Apr 2007 10:56:01 +0000 Subject: [PATCH] Extend Numeric class with a clip() method to force a number to be in a given range --- lib/rbot/core/utils/extends.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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 -- 2.39.2