Skip to content

Commit 19197c0

Browse files
committed
Add the time comparison guideline
1 parent 524a1ac commit 19197c0

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

README.adoc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,6 +1808,36 @@ date.all_quarter
18081808
date.all_year
18091809
----
18101810

1811+
=== Comparison
1812+
1813+
Avoid comparing time with `>` and `<` to avoid confusion.
1814+
1815+
[source,ruby]
1816+
----
1817+
# bad
1818+
created_at < 5.minutes.ago
1819+
shutdown_at > 1.minute.from_now
1820+
1821+
# good
1822+
created_at.before?(5.minutes.ago)
1823+
shutdown_at.after?(1.minute.from_now)
1824+
----
1825+
1826+
Use `past?` and `future?` when comparing with the current time.
1827+
1828+
[source,ruby]
1829+
----
1830+
# bad
1831+
grand_opening_at < Time.current
1832+
public_release_at > Time.current
1833+
1834+
# good
1835+
grand_opening_at.past?
1836+
public_release_at.future?
1837+
----
1838+
1839+
NOTE: Rails 6.0 or later is required to use `before?` and `after?`.
1840+
18111841
== Duration
18121842

18131843
=== Duration Application

0 commit comments

Comments
 (0)