Method: Time#compare_with_coercion

Defined in:
activesupport/lib/active_support/core_ext/time/calculations.rb

#compare_with_coercion(other) ⇒ Object Also known as: <=>

Layers additional behavior on Time#<=> so that DateTime and ActiveSupport::TimeWithZone instances can be chronologically compared with a Time



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'activesupport/lib/active_support/core_ext/time/calculations.rb', line 329

def compare_with_coercion(other)
  # we're avoiding Time#to_datetime and Time#to_time because they're expensive
  if other.class == Time
    compare_without_coercion(other)
  elsif other.is_a?(Time)
    # also avoid ActiveSupport::TimeWithZone#to_time before Rails 8.0
    if other.respond_to?(:comparable_time)
      compare_without_coercion(other.comparable_time)
    else
      compare_without_coercion(other.to_time)
    end
  else
    to_datetime <=> other
  end
end