Skip to content

Instantly share code, notes, and snippets.

@tiegz
Created December 1, 2008 17:39
Show Gist options
  • Save tiegz/30777 to your computer and use it in GitHub Desktop.
Save tiegz/30777 to your computer and use it in GitHub Desktop.
module TrueFalseComparison
def <=>(other)
return nil if ![TrueClass, FalseClass].include?(other.class)
self.is_a?(FalseClass) ? (other.is_a?(TrueClass) ? -1 : 0) : (other.is_a?(FalseClass) ? 1 : 0)
end
end
TrueClass.send(:include, TrueFalseComparison)
FalseClass.send(:include, TrueFalseComparison)
>> true <=> false
=> 1
>> true <=> true
=> 0
>> true <=> 'asdf'
=> nil
>> false <=> false
=> 0
>> false <=> true
=> -1
>> false <=> 'asdf'
=> nil
>> [1,2,3,4].sort_by { |n| n==3 }
=> [1, 2, 4, 3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment