From f1d893f8348fa31153ec625776d81edced301c15 Mon Sep 17 00:00:00 2001 From: Thomas Klemm Date: Wed, 4 Nov 2026 17:10:25 +0000 Subject: [PATCH] Return nil from Date#<=> for a NaN 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. --- Gemfile | 1 + ext/date/date_core.c | 2 +- test/date/test_date_arith.rb | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index dbc00c9a..07485320 100644 --- a/Gemfile +++ b/Gemfile @@ -4,6 +4,7 @@ source 'https://rubygems.org' gemspec unless RUBY_ENGINE == 'jruby' group :development do + gem "bigdecimal" gem "bundler" gem "rake" gem "rake-compiler" diff --git a/ext/date/date_core.c b/ext/date/date_core.c index 575e0e49..22946d46 100644 --- a/ext/date/date_core.c +++ b/ext/date/date_core.c @@ -6920,7 +6920,7 @@ cmp_gen(VALUE self, VALUE other) get_d1(self); if (k_numeric_p(other)) - return INT2FIX(f_cmp(m_ajd(dat), other)); + return rb_funcallv(m_ajd(dat), id_cmp, 1, &other); else if (k_date_p(other)) return INT2FIX(f_cmp(m_ajd(dat), f_ajd(other))); return rb_num_coerce_cmp(self, other, id_cmp); diff --git a/test/date/test_date_arith.rb b/test/date/test_date_arith.rb index c3633c94..ca9b553c 100644 --- a/test/date/test_date_arith.rb +++ b/test/date/test_date_arith.rb @@ -97,6 +97,21 @@ def test__compare DateTime.new(2001,1,17,0,0,0,0,Date::GREGORIAN))) end + def test__compare_nan + nans = [Float::NAN] + begin + require "bigdecimal" + nans << BigDecimal("NaN") + rescue LoadError + end + d = Date.new(2000,1,1) + nans.each do |nan| + assert_nil(d <=> nan) + assert_equal(false, d == nan) + assert_raise(ArgumentError) { d < nan } + end + end + def test_prev d = Date.new(2000,1,1) assert_raise(NoMethodError) do