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
+31-27Lines changed: 31 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1535,43 +1535,47 @@ Time.current # Same thing but shorter.
1535
1535
1536
1536
== Duration
1537
1537
1538
-
Don't use `.since`, `.after` without a parameter, prefer `.from_now`.
1538
+
If used without a parameter, prefer `from_now` and `ago` instead of `since`, `after`, `until` or `before`.
1539
1539
1540
-
Dont't use `.until`, `.before`, without a parameter, prefer `ago`.
1541
-
1542
-
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`
1540
+
[source,ruby]
1541
+
----
1542
+
# bad - It's not clear at first sight that the qualifier refers to the current time (which is the default parameter)
1543
+
5.hours.since
1544
+
5.hours.after
1545
+
5.hours.before
1546
+
5.hours.until
1543
1547
1544
-
Don't use hard coded negative numbers for the duration subject
1548
+
# good
1549
+
5.hours.from_now
1550
+
5.hours.ago
1551
+
----
1545
1552
1546
-
As a general rule, make sure that reading the code as it was plain english makes total sense.
1553
+
If used with a parameter, prefer `since`, `after`, `until` or `before` instead of `from_now` and `ago`.
1547
1554
1548
1555
[source,ruby]
1549
1556
----
1550
-
# bad
1551
-
5.hours.since # => It's not clear at first sight that `since` refers to "since now"
1552
-
5.hours.after # => It's not clear at first sight that `after` refers to "after now"
1553
-
5.hours.before # => It's not clear at first sight that `before` refers to "before now"
1554
-
5.hours.until # => It's not clear at first sight that `until` refers to "until now"
1555
-
1556
-
tomorrow = 2.days.from_now(yesterday) # yesterday would be 1.day.ago, reading this is confusing
1557
-
today = 1.day.ago(tomorrow)
1557
+
# bad - It's confusing and misleading to read
1558
+
2.days.from_now(yesterday)
1559
+
2.days.ago(yesterday)
1558
1560
1559
-
1560
-
-5.hours.from_now # reading this is confusing
1561
-
-5.hours.ago # reading this is confusing
1562
-
-5.hours.after(today)
1563
1561
# good
1564
-
5.hours.from_now # when refering a future time from now, use from_now
1565
-
5.hours.ago # when refering a past time from now, use ago
1562
+
2.days.since(yesterday)
1563
+
2.days.after(yesterday)
1564
+
2.days.before(yesterday)
1565
+
2.days.until(yesterday)
1566
+
----
1567
+
1568
+
Avoid using literal negative numbers for the duration subject. Always prefer using a qualifier that allows using positive literal numbers.
1566
1569
1567
-
tomorrow = 2.days.since(yesterday) # use since only when you have a referential Time in the past
1568
-
tomorrow = 2.days.after(yesterday) # use after only when you have a referential Time in the past
1569
-
today = 1.day.before(tomorrow) # use before only when you have a referential Time in the future
1570
-
today = 1.day.until(tomorrow) # use until only when you have a referential Time in the future
1570
+
[source,ruby]
1571
+
----
1572
+
# bad - It's confusing and misleading to read
1573
+
-5.hours.from_now
1574
+
-5.hours.ago
1571
1575
1572
-
5.hours.ago # use the inverse method instead of the inverse number, -5.hours.from_now becomes 5.hours.ago
1573
-
5.hours.ago # use the inverse method instead of the inverse number, -5.hours.ago becomes 5.hours.ago
0 commit comments