Guest User

Untitled

a guest
Feb 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. module TrueFalseComparison
  2. def <=>(other)
  3. return nil if ![TrueClass, FalseClass].include?(other.class)
  4.  
  5. self.is_a?(FalseClass) ? (other.is_a?(TrueClass) ? -1 : 0) : (other.is_a?(FalseClass) ? 1 : 0)
  6. end
  7. end
  8. TrueClass.send(:include, TrueFalseComparison)
  9. FalseClass.send(:include, TrueFalseComparison)
  10.  
  11. >> true <=> false
  12. => 1
  13. >> true <=> true
  14. => 0
  15. >> true <=> 'asdf'
  16. => nil
  17. >> false <=> false
  18. => 0
  19. >> false <=> true
  20. => -1
  21. >> false <=> 'asdf'
  22. => nil
  23. >> [1,2,3,4].sort_by { |n| n==3 }
  24. => [1, 2, 4, 3]
Add Comment
Please, Sign In to add comment