1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-15 13:08:02 +00:00

Compare commits

...

4 Commits

Author SHA1 Message Date
Bob Conan
e75cadbb88 Merge 5615da920f into 4f2399de13 2024-11-22 10:48:37 -05:00
jbranchaud
4f2399de13 Add Useful ActiveSupport Constants For Durations as a Rails TIL 2024-11-22 09:45:20 -06:00
Bob Conan
5615da920f Update README.md, fix typos 2024-11-15 16:16:31 -06:00
BobConanDev
c60c63f554 Updated README.md, fix typo(s) 2024-11-15 16:42:57 -05:00
2 changed files with 49 additions and 3 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1513 TILs and counting..._
_1514 TILs and counting..._
---
@@ -190,7 +190,7 @@ _1513 TILs and counting..._
- [Aliasing An Ansible Host](devops/aliasing-an-ansible-host.md)
- [Allow Cross-Origin Requests To Include Cookies](devops/allow-cross-origin-requests-to-include-cookies.md)
- [Allow HTTPS Through Your UFW Firewall](devops/allow-https-through-your-ufw-firewall.md)
- [Check For Cached Site Assocation File For iOS](devops/check-for-cached-site-association-file-for-ios.md)
- [Check For Cached Site Association File For iOS](devops/check-for-cached-site-association-file-for-ios.md)
- [Check The Status of All Services](devops/check-the-status-of-all-services.md)
- [Check The Syntax Of nginx Files](devops/check-the-syntax-of-nginx-files.md)
- [Connect To An RDS PostgreSQL Database](devops/connect-to-an-rds-postgresql-database.md)
@@ -723,7 +723,7 @@ _1513 TILs and counting..._
- [Check If Clusters Are Upgrade Compatible](postgres/check-if-clusters-are-upgrade-compatible.md)
- [Check If The Local Server Is Running](postgres/check-if-the-local-server-is-running.md)
- [Check If User Role Exists For Database](postgres/check-if-user-role-exists-for-database.md)
- [Check Table For Any Oprhaned Records](postgres/check-table-for-any-orphaned-records.md)
- [Check Table For Any Orphaned Records](postgres/check-table-for-any-orphaned-records.md)
- [Checking Inequality](postgres/checking-inequality.md)
- [Checking The Type Of A Value](postgres/checking-the-type-of-a-value.md)
- [Clear The Screen In psql](postgres/clear-the-screen-in-psql.md)
@@ -1040,6 +1040,7 @@ _1513 TILs and counting..._
- [Update Column Versus Update Attribute](rails/update-column-versus-update-attribute.md)
- [Upgrading Your Manifest For Sprocket's 4](rails/upgrading-your-manifest-for-sprockets-4.md)
- [Use IRB And Ruby Flags With Rails Console](rails/use-irb-and-ruby-flags-with-rails-console.md)
- [Useful ActiveSupport Constants For Durations](rails/useful-active-support-constants-for-durations.md)
- [Validate Column Data With Check Constraints](rails/validate-column-data-with-check-constraints.md)
- [Verify And Read A Signed Cookie Value](rails/verify-and-read-a-signed-cookie-value.md)
- [Where Am I In The Partial Iteration?](rails/where-am-i-in-the-partial-iteration.md)

View File

@@ -0,0 +1,45 @@
# Useful ActiveSupport Constants For Durations
Whenever I'm passing a duration to a function, I like to [name it with the
unit](https://ruudvanasseldonk.com/2022/03/20/please-put-units-in-names)
relative to the value it represents. For instance, if I need to pass in an hour
duration in seconds, I might write the following line:
```ruby
hour_in_seconds = 60 * 60
# or
hour_in_seconds = 3600
```
ActiveSupport has a [Duration
class](https://api.rubyonrails.org/classes/ActiveSupport/Duration.html) with a
series of constants that we can reach for.
```ruby
> ActiveSupport::Duration::SECONDS_PER_MINUTE
=> 60
> ActiveSupport::Duration::SECONDS_PER_HOUR
=> 3600
> ActiveSupport::Duration::SECONDS_PER_DAY
=> 86400
> ActiveSupport::Duration::SECONDS_PER_WEEK
=> 604800
> ActiveSupport::Duration::SECONDS_PER_MONTH
=> 2629746
> ActiveSupport::Duration::SECONDS_PER_YEAR
=> 31556952
```
Though it is fun to know about these, we should keep in mind that this class
provides support for what is likely to be a more useful abstraction layer:
```ruby
> 1.hour
=> 1 hour
> 3.hours
=> 3 hours
> 1.day.to_i
=> 86400
```