Permalink
Browse files

Allow Date to be compared with Time (like it was possible to compare …

…Time with Date)
  • Loading branch information...
1 parent bb3d9a6 commit a6fe33d8a3c09f3a70ee2a79f96ed5b2c9580d4f @dhh dhh committed Jun 3, 2013
@@ -119,4 +119,15 @@ def change(options)
options.fetch(:day, day)
)
end
+
+ # Allow Dates to be compared with times. The Date will have its time set to 00:00:00 for the comparison.
+ def compare_with_coercion(other)
+ if other.is_a?(Time)
+ self.to_time <=> other
+ else
+ compare_without_coercion(other)
+ end
+ end
+ alias_method :compare_without_coercion, :<=>
+ alias_method :<=>, :compare_with_coercion
end
@@ -48,6 +48,10 @@ def test_to_time
end
end
+ def test_compare_to_time
+ assert Date.yesterday < Time.now
+ end
+
def test_to_datetime
assert_equal DateTime.civil(2005, 2, 21), Date.new(2005, 2, 21).to_datetime
assert_equal 0, Date.new(2005, 2, 21).to_datetime.offset # use UTC offset

0 comments on commit a6fe33d

Please sign in to comment.