diff options
Diffstat (limited to 'compar.c')
-rw-r--r-- | compar.c | 59 |
1 files changed, 33 insertions, 26 deletions
@@ -30,13 +30,13 @@ rb_cmperr(VALUE x, VALUE y) VALUE classname; if (SPECIAL_CONST_P(y) || BUILTIN_TYPE(y) == T_FLOAT) { - classname = rb_inspect(y); + classname = rb_inspect(y); } else { - classname = rb_obj_class(y); + classname = rb_obj_class(y); } rb_raise(rb_eArgError, "comparison of %"PRIsVALUE" with %"PRIsVALUE" failed", - rb_obj_class(x), classname); + rb_obj_class(x), classname); } static VALUE @@ -50,12 +50,12 @@ VALUE rb_invcmp(VALUE x, VALUE y) { VALUE invcmp = rb_exec_recursive(invcmp_recursive, x, y); - if (invcmp == Qundef || NIL_P(invcmp)) { - return Qnil; + if (NIL_OR_UNDEF_P(invcmp)) { + return Qnil; } else { - int result = -rb_cmpint(invcmp, x, y); - return INT2FIX(result); + int result = -rb_cmpint(invcmp, x, y); + return INT2FIX(result); } } @@ -167,9 +167,7 @@ cmp_le(VALUE x, VALUE y) static VALUE cmp_between(VALUE x, VALUE min, VALUE max) { - if (cmpint(x, min) < 0) return Qfalse; - if (cmpint(x, max) > 0) return Qfalse; - return Qtrue; + return RBOOL((cmpint(x, min) >= 0 && cmpint(x, max) <= 0)); } /* @@ -189,6 +187,12 @@ cmp_between(VALUE x, VALUE min, VALUE max) * 'd'.clamp('a', 'f') #=> 'd' * 'z'.clamp('a', 'f') #=> 'f' * + * If _min_ is +nil+, it is considered smaller than _obj_, + * and if _max_ is +nil+, it is considered greater than _obj_. + * + * -20.clamp(0, nil) #=> 0 + * 523.clamp(nil, 100) #=> 100 + * * In <code>(range)</code> form, returns _range.begin_ if _obj_ * <code><=></code> _range.begin_ is less than zero, _range.end_ * if _obj_ <code><=></code> _range.end_ is greater than zero, and @@ -231,7 +235,7 @@ cmp_clamp(int argc, VALUE *argv, VALUE x) } } if (!NIL_P(min) && !NIL_P(max) && cmpint(min, max) > 0) { - rb_raise(rb_eArgError, "min argument must be smaller than max argument"); + rb_raise(rb_eArgError, "min argument must be less than or equal to max argument"); } if (!NIL_P(min)) { @@ -259,25 +263,28 @@ cmp_clamp(int argc, VALUE *argv, VALUE x) * <code>==</code>, <code>>=</code>, and <code>></code>) and the * method <code>between?</code>. * - * class SizeMatters + * class StringSorter * include Comparable + * * attr :str * def <=>(other) * str.size <=> other.str.size * end + * * def initialize(str) * @str = str * end + * * def inspect * @str * end * end * - * s1 = SizeMatters.new("Z") - * s2 = SizeMatters.new("YY") - * s3 = SizeMatters.new("XXX") - * s4 = SizeMatters.new("WWWW") - * s5 = SizeMatters.new("VVVVV") + * s1 = StringSorter.new("Z") + * s2 = StringSorter.new("YY") + * s3 = StringSorter.new("XXX") + * s4 = StringSorter.new("WWWW") + * s5 = StringSorter.new("VVVVV") * * s1 < s2 #=> true * s4.between?(s1, s3) #=> false @@ -288,18 +295,18 @@ cmp_clamp(int argc, VALUE *argv, VALUE x) * * \Module \Comparable provides these methods, all of which use method <tt><=></tt>: * - * - {<}[#method-i-3C]:: Returns whether +self+ is less than the given object. - * - {<=}[#method-i-3C-3D]:: Returns whether +self+ is less than or equal to - * the given object. - * - {==}[#method-i-3D-3D]:: Returns whether +self+ is equal to the given object. - * - {>}[#method-i-3E]:: Returns whether +self+ is greater than or equal to - * the given object. - * - {>=}[#method-i-3E-3D]:: Returns whether +self+ is greater than the given object. - * - #between? Returns +true+ if +self+ is between two given objects. - * - #clamp:: For given objects +min+ and +max+, or range <tt>(min..max)</tt>, returns: + * - #<: Returns whether +self+ is less than the given object. + * - #<=: Returns whether +self+ is less than or equal to the given object. + * - #==: Returns whether +self+ is equal to the given object. + * - #>: Returns whether +self+ is greater than the given object. + * - #>=: Returns whether +self+ is greater than or equal to the given object. + * - #between?: Returns +true+ if +self+ is between two given objects. + * - #clamp: For given objects +min+ and +max+, or range <tt>(min..max)</tt>, returns: + * * - +min+ if <tt>(self <=> min) < 0</tt>. * - +max+ if <tt>(self <=> max) > 0</tt>. * - +self+ otherwise. + * */ void |