ruby/ruby — pull request #392 by ernie (2013-09-07 02:41:52+00:00)
Title: Improve comparison error message.

In certain cases, things like Array#sort can result in a confusing error
message. For instance where a and b are characters in a string,
"string":

array.sort { |a, b| string.index(a) <=> string.index(b)

If one of the index calls returns nil, we will get "comparison of String
with String failed", which is somewhat unhelpful, since it's easy to be
confused, given that what is really being compared is a Fixnum or
NilClass (the cause of the error). Yes, as far as Array#sort is
concerned, the two characters are the things being sorted, but it's
useful to call attention to the return value of the comparison in this
case.

This patch adds a "reason" argument to rb_cmperr, which will provide an
error message of "comparison of String with String failed: comparator
returned nil" in the case above, or, in the case of:

1.upto('10').to_a

it will provide the message: "comparison of Fixnum with String failed:
coercion was not possible"