From 2d749f52750930d7dba007cd16641f6d884c11e2 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sun, 17 Jul 2022 18:53:55 +0900 Subject: [PATCH] mruby-compar-ext/compar.rb (clamp): update document; ref #5748 --- mrbgems/mruby-compar-ext/mrblib/compar.rb | 45 +++++++++++++++++++---- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/mrbgems/mruby-compar-ext/mrblib/compar.rb b/mrbgems/mruby-compar-ext/mrblib/compar.rb index 0ea3d190b..3b1fdb7f8 100644 --- a/mrbgems/mruby-compar-ext/mrblib/compar.rb +++ b/mrbgems/mruby-compar-ext/mrblib/compar.rb @@ -1,15 +1,44 @@ module Comparable ## - # Returns min if obj <=> min is less - # than zero, max if obj <=> max is - # greater than zero and obj otherwise. + # call-seq: + # obj.clamp(min, max) -> obj + # obj.clamp(range) -> obj # - # 12.clamp(0, 100) #=> 12 - # 523.clamp(0, 100) #=> 100 - # -3.123.clamp(0, 100) #=> 0 + # In (min, max) form, returns _min_ if _obj_ + # <=> _min_ is less than zero, _max_ if _obj_ + # <=> _max_ is greater than zero, and _obj_ + # otherwise. # - # 'd'.clamp('a', 'f') #=> 'd' - # 'z'.clamp('a', 'f') #=> 'f' + # 12.clamp(0, 100) #=> 12 + # 523.clamp(0, 100) #=> 100 + # -3.123.clamp(0, 100) #=> 0 + # + # 'd'.clamp('a', 'f') #=> 'd' + # 'z'.clamp('a', 'f') #=> 'f' + # + # In (range) form, returns _range.begin_ if _obj_ + # <=> _range.begin_ is less than zero, _range.end_ + # if _obj_ <=> _range.end_ is greater than zero, and + # _obj_ otherwise. + # + # 12.clamp(0..100) #=> 12 + # 523.clamp(0..100) #=> 100 + # -3.123.clamp(0..100) #=> 0 + # + # 'd'.clamp('a'..'f') #=> 'd' + # 'z'.clamp('a'..'f') #=> 'f' + # + # If _range.begin_ is +nil+, it is considered smaller than _obj_, + # and if _range.end_ is +nil+, it is considered greater than + # _obj_. + # + # -20.clamp(0..) #=> 0 + # 523.clamp(..100) #=> 100 + # + # When _range.end_ is excluded and not +nil+, an exception is + # raised. + # + # 100.clamp(0...100) # ArgumentError # def clamp(min, max=nil) if max.nil?