From 3adae7d2375e21e2009c93ff9f3690d1ebaca51f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?fn=20=E2=8C=83=20=E2=8C=A5?= <70830482+FnControlOption@users.noreply.github.com> Date: Wed, 26 Oct 2022 08:55:29 -0700 Subject: [PATCH] mruby-compar-ext/compar.rb (clamp): fix range support. Also, raise error if any comparison among min, max, and self returns nil. --- mrbgems/mruby-compar-ext/mrblib/compar.rb | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/mrbgems/mruby-compar-ext/mrblib/compar.rb b/mrbgems/mruby-compar-ext/mrblib/compar.rb index 3b1fdb7f8..18bba7919 100644 --- a/mrbgems/mruby-compar-ext/mrblib/compar.rb +++ b/mrbgems/mruby-compar-ext/mrblib/compar.rb @@ -43,13 +43,13 @@ module Comparable def clamp(min, max=nil) if max.nil? if min.kind_of?(Range) - max = min.begin + max = min.end if max.nil? max = self elsif min.exclude_end? raise ArgumentError, "cannot clamp with an exclusive range" end - min = min.end + min = min.begin if min.nil? min = self end @@ -57,17 +57,24 @@ module Comparable raise TypeError, "wrong argument type #{min.class}" end end - if (min <=> max) > 0 + c = min <=> max + if c.nil? + raise ArgumentError, "comparison of #{min.class} with #{max.class} failed" + elsif c > 0 raise ArgumentError, "min argument must be smaller than max argument" end c = self <=> min - if c == 0 + if c.nil? + raise ArgumentError, "comparison of #{self.class} with #{min.class} failed" + elsif c == 0 return self elsif c < 0 return min end c = self <=> max - if c > 0 + if c.nil? + raise ArgumentError, "comparison of #{self.class} with #{max.class} failed" + elsif c > 0 return max else return self