mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
mruby-compar-ext/compar.rb (clamp): fix range support.
Also, raise error if any comparison among min, max, and self returns nil.
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user