Repository navigation
Option orders like Rust: None before Some - #31
Merged
Merged
Conversation
<=> previously fell back to Object, returning nil for every pair, so sort/min/max over Options raised ArgumentError. Implement <=> and include Comparable: None < Some, Somes order by inner value, and nil for incomparable operands per Ruby convention. Fixes #22.
nz
enabled auto-merge
August 7, 2026 18:14
Same treatment as Option: implement <=> and include Comparable, with Ok < Err per Rust's variant ordering, same variants ordering by inner value, and nil for incomparable operands per Ruby convention.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #22.
<=>plusComparableon both containers, matching Rust's derivedOrd:None < Some,Somes order by inner value —[Some(2), None(), Some(1)].sortgives[None(), Some(1), Some(2)].Ok < Err(Rust's variant order), same variants order by inner value —[Err(:a), Ok(2), Ok(1)].sortgives[Ok(1), Ok(2), Err(:a)].One deliberate departure from the issue's suggestion: incomparable operands (non-container other, or inner values that don't mutually compare — e.g. two
Errs wrapping exceptions) returnnilrather than raisingNotComparableError. That is Ruby's<=>contract (1 <=> "x"isnil), andsort/minthen raise their standardArgumentError— a custom raise from<=>itself would make these objects behave unlike every other Ruby operand. Happy to revisit if a clearer sort-time error is worth the unconventional<=>.Specified as doctests, including the issue's
sortandminexamples.