Permalink
Browse files

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

…Time with Date)
  • Loading branch information...
1 parent 81b3635 commit df3c20fc334f2e9365eaf2c297e1b62a9bd97049 @dhh dhh committed Jun 3, 2013
@@ -1,5 +1,9 @@
## unreleased ##
+* Allow Date to be compared with Time (like it was possible to compare Time with Date).
+
+ *DHH*
+
* Deprecate multiple parameters support of `Object#in?`.
*Brian Morearty + Carlos Antonio da Silva*
@@ -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 df3c20f

Please sign in to comment.