What is the Comparable module and how does it work with the spaceship operator?

Last updated 27 June 2026

  • Comparable is a mixin that provides <, <=, ==, >=, >, plus between? and clamp, all derived from one method.

  • You define the spaceship operator <=> to return -1, 0, or 1 (less, equal, greater), or nil if the objects are not comparable.

  • https://docs.ruby-lang.org/en/3.2/Comparable.html
  • https://docs.ruby-lang.org/en/3.4/syntax/methods_rdoc.html
  • https://www.mintbit.com/blog/ruby-internals-the-spaceship-operator/
  • https://www.mintbit.com/blog/
  • https://goodspace.ai/interview-questions/ruby
  • https://reintech.io/blog/strategies-for-answering-ruby-interview-questions-for-software-developers
  • https://dev.to/baweaver/understanding-ruby-comparable-34ki
All Ruby interview questions