You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.adoc
+23-4Lines changed: 23 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1535,20 +1535,39 @@ Time.current # Same thing but shorter.
1535
1535
1536
1536
== Duration
1537
1537
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.
1541
1543
1542
1544
[source,ruby]
1543
1545
----
1544
1546
# bad
1545
1547
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)
1546
1554
1555
+
1556
+
-5.hours.from_now # reading this is confusing
1557
+
-5.hours.ago # reading this is confusing
1558
+
-5.hours.after(today)
1547
1559
# good
1548
1560
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
1549
1562
1550
-
yesterday = 1.day.ago
1551
1563
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
0 commit comments