diff options
Diffstat (limited to 'compar.c')
| -rw-r--r-- | compar.c | 91 |
1 files changed, 66 insertions, 25 deletions
@@ -24,8 +24,8 @@ rb_cmp(VALUE x, VALUE y) return rb_funcallv(x, idCmp, 1, &y); } -void -rb_cmperr(VALUE x, VALUE y) +static VALUE +cmperr_subject(VALUE y) { VALUE classname; @@ -35,10 +35,25 @@ rb_cmperr(VALUE x, VALUE y) else { classname = rb_obj_class(y); } + return classname; +} + +void +rb_cmperr(VALUE x, VALUE y) +{ + VALUE classname = cmperr_subject(y); rb_raise(rb_eArgError, "comparison of %"PRIsVALUE" with %"PRIsVALUE" failed", rb_obj_class(x), classname); } +void +rb_cmperr_reason(VALUE x, VALUE y, const char *reason) +{ + VALUE classname = cmperr_subject(y); + rb_raise(rb_eArgError, "comparison of %"PRIsVALUE" with %"PRIsVALUE" failed: %s", + rb_obj_class(x), classname, reason); +} + static VALUE invcmp_recursive(VALUE x, VALUE y, int recursive) { @@ -50,7 +65,7 @@ VALUE rb_invcmp(VALUE x, VALUE y) { VALUE invcmp = rb_exec_recursive(invcmp_recursive, x, y); - if (invcmp == Qundef || NIL_P(invcmp)) { + if (NIL_OR_UNDEF_P(invcmp)) { return Qnil; } else { @@ -95,10 +110,13 @@ cmpint(VALUE x, VALUE y) /* * call-seq: - * obj > other -> true or false + * self > other -> true or false * - * Compares two objects based on the receiver's <code><=></code> - * method, returning true if it returns a value greater than 0. + * Returns whether +self+ is "greater than" +other+; + * equivalent to <tt>(self <=> other) > 0</tt>: + * + * 'foo' > 'foo' # => false + * 'food' > 'foo' # => true */ static VALUE @@ -109,10 +127,15 @@ cmp_gt(VALUE x, VALUE y) /* * call-seq: - * obj >= other -> true or false + * self >= other -> true or false + * + * Returns whether +self+ is "greater than or equal to" +other+; + * equivalent to <tt>(self <=> other) >= 0</tt>: + * + * 'food' >= 'foo' # => true + * 'foo' >= 'foo' # => true + * 'foo' >= 'food' # => false * - * Compares two objects based on the receiver's <code><=></code> - * method, returning true if it returns a value greater than or equal to 0. */ static VALUE @@ -123,10 +146,14 @@ cmp_ge(VALUE x, VALUE y) /* * call-seq: - * obj < other -> true or false + * self < other -> true or false + * + * Returns whether +self+ is "less than" +other+; + * equivalent to <tt>(self <=> other) < 0</tt>: + * + * 'foo' < 'foo' # => false + * 'foo' < 'food' # => true * - * Compares two objects based on the receiver's <code><=></code> - * method, returning true if it returns a value less than 0. */ static VALUE @@ -137,10 +164,15 @@ cmp_lt(VALUE x, VALUE y) /* * call-seq: - * obj <= other -> true or false + * self <= other -> true or false + * + * Returns whether +self+ is "less than or equal to" +other+; + * equivalent to <tt>(self <=> other) <= 0</tt>: + * + * 'foo' <= 'foo' # => true + * 'foo' <= 'food' # => true + * 'food' <= 'foo' # => false * - * Compares two objects based on the receiver's <code><=></code> - * method, returning true if it returns a value less than or equal to 0. */ static VALUE @@ -187,6 +219,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 @@ -229,7 +267,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)) { @@ -257,25 +295,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 @@ -284,13 +325,13 @@ cmp_clamp(int argc, VALUE *argv, VALUE x) * * == What's Here * - * \Module \Comparable provides these methods, all of which use method <tt><=></tt>: + * Module \Comparable provides these methods, all of which use method <tt>#<=></tt>: * * - #<: 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 or equal to the given object. - * - #>=: Returns whether +self+ is greater than 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: * |
