Rails 6 adds before?
and after?
methods to Date
, DateTime
, Time
, and TimeWithZone
.
We read >
and <
as “greater than” and “less than” in ruby. before?
and after?
methods make the date/time comparisons much more human-readable.
Before
We can compare two date or time objects using >
and <
.
This is not so easy to read. We read it as Date.tomorrow
is “greater than” Date.today
.
before? and after? methods
When comparing dates in English, one would say:
The due date for the assignment is before the end of the month. All submissions after the end date will be ignored.
To match these, we now have before?
and after?
comparison methods in ActiveSupport
.
We can now compare date and time values like so:
The comparison is now much more readable and reads like: Is due date before today or after today?