Using the Spaceship operator with TrueClass / FalseClass is not possible normally:
But we can change this…
Code
module TrueFalseComparison def <=>(other) raise ArgumentError unless [TrueClass, FalseClass].include?(other.class) other ? (self ? 0 : -1) : (self ? 1 : 0) end end TrueClass.send(:include, TrueFalseComparison) FalseClass.send(:include, TrueFalseComparison)
Usage
(true <=> false) == 1 (false <=> true) == -1 [true,false,true].sort == [false,true,true] [true,'x'].sort -> ArgumentError
Wouldn’t you expect ‘true’ to be on top of the sorted list?
Yes, when first thinking of it i did, but it should behave like [0,1].sort -> having the ‘low’ value on top.