instance method
Date#<=>
(untyped other) -> ::Integer?
self <=> other → -1, 0, 1 or nil
ext/date/date_core.c
View on GitHub ↗
static VALUE
d_lite_cmp(VALUE self, VALUE other)
{
if (!k_date_p(other))
return cmp_gen(self, other);
{
get_d2(self, other);
if (!(simple_dat_p(adat) && simple_dat_p(bdat) &&
m_gregorian_p(adat) == m_gregorian_p(bdat)))
return cmp_dd(self, other);
{
VALUE a_nth, b_nth;
int a_jd, b_jd;
m_canonicalize_jd(self, adat);
m_canonicalize_jd(other, bdat);
a_nth = m_nth(adat);
b_nth = m_nth(bdat);
if (f_eqeq_p(a_nth, b_nth)) {
a_jd = m_jd(adat);
b_jd = m_jd(bdat);
if (a_jd == b_jd) {
return INT2FIX(0);
}
else if (a_jd < b_jd) {
return INT2FIX(-1);
}
else {
return INT2FIX(1);
}
}
else if (f_lt_p(a_nth, b_nth)) {
return INT2FIX(-1);
}
else {
return INT2FIX(1);
}
}
}
}
Compares self and other, returning:
-
-1ifotheris larger. -
0if the two are equal. -
1ifotheris smaller. -
nilif the two are incomparable.
Argument other may be:
-
Another Date object:
Exampled = Date.new(2022, 7, 27) # => #<Date: 2022-07-27 ((2459788j,0s,0n),+0s,2299161j)> prev_date = d.prev_day # => #<Date: 2022-07-26 ((2459787j,0s,0n),+0s,2299161j)> next_date = d.next_day # => #<Date: 2022-07-28 ((2459789j,0s,0n),+0s,2299161j)> d <=> next_date # => -1 d <=> d # => 0 d <=> prev_date # => 1
-
A
DateTimeobject:Exampled <=> DateTime.new(2022, 7, 26) # => 1 d <=> DateTime.new(2022, 7, 27) # => 0 d <=> DateTime.new(2022, 7, 28) # => -1
-
A numeric (compares
self.ajdtoother):Exampled <=> 2459788 # => -1 d <=> 2459787 # => 1 d <=> 2459786 # => 1 d <=> d.ajd # => 0
-
Any other object:
Exampled <=> Object.new # => nil