Skip to content

Return nil from Date#<=> for a NaN - #199

Open
thomasklemm wants to merge 1 commit into
ruby:masterfrom
thomasklemm:thomasklemm/cmp-nan
Open

thomasklemm wants to merge 1 commit into
ruby:masterfrom
thomasklemm:thomasklemm/cmp-nan

Conversation

@thomasklemm

Copy link
Copy Markdown

Thank you for maintaining date.

Date#<=> raises ArgumentError ("comparison of Rational with NaN failed") when the other value is Float::NAN or BigDecimal("NaN"), and Date#== raises with it, while 1 <=> Float::NAN, Rational(1) <=> Float::NAN and Time.now <=> Float::NAN return nil. cmp_gen passes the result of ajd <=> other to rb_cmpint, which turns the nil from Rational#<=> into that exception.

This returns the result unchanged. Date <=> NaN is now nil and Date == NaN is false, while Date < NaN still raises ArgumentError, as for Integer. The new test covers both NaNs, so I added bigdecimal to the development group. The suite passes on Ruby 4.0.9 and master.


Found while comparing Active Model's changed? on Rails 8.1 and main with random attribute values, where a NaN assigned to a date attribute raised (record, part of my research project Lowrail).

Date#<=> passed the result of ajd <=> other to rb_cmpint, which turns
the nil that Rational#<=> returns for a NaN into an ArgumentError, so
Date <=> Float::NAN raised and Date == Float::NAN raised with it.
Return the result unchanged, as Integer#<=> and Rational#<=> do.
thomasklemm added a commit to thomasklemm/lowrail that referenced this pull request Nov 5, 2026
Move ruby-bench #569-#572 from Merged to Open (ready), drop the duplicate
with_advisory_lock#150 entry, add ruby/date#199 and the closed ferrum#697,
mark ransack#1795 and tzinfo#175 ready, refresh heads (#59614, #59668,
ruby-bench#568, ruby/ruby#19681, tzinfo#171), add missing merge dates, and
record the 2026-11-05 open counts. RA1 item synced to ready.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant