From: Giuseppe Bilotta Date: Fri, 6 Apr 2007 10:56:01 +0000 (+0000) Subject: Extend Numeric class with a clip() method to force a number to be in a given range X-Git-Url: https://git.netwichtig.de/gitweb/?a=commitdiff_plain;h=2c65a50c9fbdbdaab20d76b73e9a68023739417f;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git Extend Numeric class with a clip() method to force a number to be in a given range --- 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