From: "jEFF (Jean-François Berroyer)" Date: 2012-06-08T01:05:57+09:00 Subject: [ruby-core:45477] [ruby-trunk - Feature #6555] New comparison operators Issue #6555 has been updated by jEFF (Jean-Fran��ois Berroyer). =begin Ok, il will try to explain my mind. Imagine objects that are naturaly ordered like in a tree structure. This is typically a partial order. You would like to write (({a < b})) to test if a is a descendant of b, (({a > b})) if a is an ancestor of b, of course (({a == b})) if a and b are in the same position in the tree, but for two elements that are not in such a relationship, like sibblings, you have to write (({(a != b) && !(a < b) && !(a > b)})) or just (({(a <=> b) == nil})) or in the shorter way (({!(a <=> b)})) that is still quite a long expression. As <, > and == operators are quick ways to test -1, 1 and 0 values of <=>, I think it would be fine to have such a way to test if a and b are not comparable, that is why I suggest to introduce the >< operator. Then I thought that the Ruby principle of negative operators like != and !~, that are not real methods but shortcut to express (({!(a == b)})) or (({!(a =~ b)})), could be extended for all comparison operators. With a total order, operators like !< are not very useful because >= can do the job. But with a partial order, !< and >= are quite differents. In my exemple of tree structure, a !< b would express that a is not a child of b. It can be an ancestor, but also a sibbling. Idem for <> operator : with a total order, != do the job, but with partial order, <> and != have two different meanings. I think those new operators would be in the Ruby way, and adding them would generalize what already exists in ruby. But I am aware that in most cases, we are working with total order relations for which half of those operators are redundant. I hope I convinced you... jEFF =end ---------------------------------------- Feature #6555: New comparison operators https://bugs.ruby-lang.org/issues/6555#change-27066 Author: jEFF (Jean-Fran��ois Berroyer) Status: Open Priority: Normal Assignee: Category: core Target version: 2.0.0 =begin x <=> y returns -1, 0, 1 or nil (x <=> y) == -1 means "x less than y" and is the same as x < y (x <=> y) == 0 means "x equal y" is the same as x == y (x <=> y) == 1 means "x greater than y" and is the same as x > y (x <=> y) == nil means "x not comparable with y" and is the same as !(x <=> y) We see there is no short syntax to test if two objects are not comparable. Can we have something like (({x >< y})), provided by (({Comparable})) module, that would mean "x and y are not comparable" ? As (({x != y})) is a short syntax for (({!(x == y)})), can we have (({x !< y})) for (({!(x < y)})) and (({x !<= y})) for (({!(x <= y)})) and so on ? As (({x <= y})) is the same as (({(x < y or x == y)})), can we have (({x <> y})) for (({(x < y or x > y)})) ? Here is a list of all possible comparison operators there could be (and their meanings) : < return true when <=> return -1 ("less than") == return true when <=> return 0 ("egal") > return true when <=> return 1 ("greater than") <= return true when <=> return -1 or 0 ("equal or less than") >= return true when <=> return 1 or 0 ("equal or greater than") <> return true when <=> return 1 or -1 ("less or greater than" that is to say "comparable and different") >< return true when <=> return nil ("not comparable") !< return true when <=> return 0 or 1 or nil ("not less than", that is different from "greater than or eqal" that involves "comparable") != return true when <=> return -1 or 1 or nil ("not equal", that is different from "less or greater than" that involves "comparable") !> return true when <=> return -1 or 0 or nil ("not greater than", that is different from "less than or eqal" that involves "comparable") !<= return true when <=> return nil or 1 ("not equal nor less than", that is different from "greater than" that involves "comparable") !>= return true when <=> return nil or -1 ("not equal nor greater than", that is different from "less than" that involves "comparable") !<> return true when <=> return 0 or nil ("not less nor greater than", that is different from "egal" that involves "comparable") !>< return true when <=> return -1 or 0 or 1 ("not not comparable" or just "comparable", the same as <=> but returning a boolean) All these operators would be very useful, especially when working with partial orders (like subset-inclusion for exemple). Perhaps (({x !== y})) should also be a short syntax for (({!(x === y)})). =end -- http://bugs.ruby-lang.org/