Skip to content

Commit 72fff76

Browse files
authored
add more cases of duration preferred usage
1 parent 025e227 commit 72fff76

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

README.adoc

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,20 +1535,39 @@ Time.current # Same thing but shorter.
15351535

15361536
== Duration
15371537

1538-
=== `.since` [[duration-since]]
1539-
1540-
Don't use `.since` without a parameter, prefer `.from_now`.
1538+
Don't use `.since`, `.after` without a parameter, prefer `.from_now`.
1539+
Dont't use `.until`, `.before`, without a parameter, prefer `ago`.
1540+
Dont't use `.ago' or `.from_now` with a parameter, prefer their respective aliases `.until` or `.before` for `ago` and `.since` or `.after` for `from_now`
1541+
Don't use hard coded negative numbers for the duration subject
1542+
As a general rule, make sure that reading the code as it was plain english makes total sense.
15411543

15421544
[source,ruby]
15431545
----
15441546
# bad
15451547
5.hours.since # => It's not clear at first sight that `since` refers to "since now"
1548+
5.hours.after # => It's not clear at first sight that `after` refers to "after now"
1549+
5.hours.before # => It's not clear at first sight that `before` refers to "before now"
1550+
5.hours.until # => It's not clear at first sight that `until` refers to "until now"
1551+
1552+
tomorrow = 2.days.from_now(yesterday) # yesterday would be 1.day.ago, reading this is confusing
1553+
today = 1.day.ago(tomorrow)
15461554
1555+
1556+
-5.hours.from_now # reading this is confusing
1557+
-5.hours.ago # reading this is confusing
1558+
-5.hours.after(today)
15471559
# good
15481560
5.hours.from_now # when refering a future time from now, use from_now
1561+
5.hours.ago # when refering a past time from now, use ago
15491562
1550-
yesterday = 1.day.ago
15511563
tomorrow = 2.days.since(yesterday) # use since only when you have a referential Time in the past
1564+
tomorrow = 2.days.after(yesterday) # use after only when you have a referential Time in the past
1565+
today = 1.day.before(tomorrow) # use before only when you have a referential Time in the future
1566+
today = 1.day.until(tomorrow) # use until only when you have a referential Time in the future
1567+
1568+
5.hours.ago # use the inverse method instead of the inverse number, -5.hours.from_now becomes 5.hours.ago
1569+
5.hours.ago # use the inverse method instead of the inverse number, -5.hours.ago becomes 5.hours.ago
1570+
5.hours.before(today)
15521571
----
15531572

15541573
== Bundler

0 commit comments

Comments
 (0)