mirror of
https://github.com/jbranchaud/til
synced 2026-01-16 13:38:02 +00:00
Compare commits
6 Commits
a9a6de8020
...
c094019937
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c094019937 | ||
|
|
633c1fa0a5 | ||
|
|
96c394c198 | ||
|
|
0251157dc4 | ||
|
|
5615da920f | ||
|
|
c60c63f554 |
@@ -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).
|
||||
|
||||
_1582 TILs and counting..._
|
||||
_1585 TILs and counting..._
|
||||
|
||||
See some of the other learning resources I work on:
|
||||
- [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators)
|
||||
@@ -195,7 +195,7 @@ See some of the other learning resources I work on:
|
||||
- [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)
|
||||
@@ -768,7 +768,7 @@ See some of the other learning resources I work on:
|
||||
- [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)
|
||||
@@ -993,6 +993,7 @@ See some of the other learning resources I work on:
|
||||
- [Define The Root Path For The App](rails/define-the-root-path-for-the-app.md)
|
||||
- [Delete Paranoid Records](rails/delete-paranoid-records.md)
|
||||
- [Demodulize A Class Name](rails/demodulize-a-class-name.md)
|
||||
- [Determine The Configured Primary Key Type](rails/determine-the-configured-primary-key-type.md)
|
||||
- [Different Ways To Add A Foreign Key Reference](rails/different-ways-to-add-a-foreign-key-reference.md)
|
||||
- [Disambiguate Where In A Joined Relation](rails/disambiguate-where-in-a-joined-relation.md)
|
||||
- [Empty find_by Returns First Record](rails/empty-find-by-returns-first-record.md)
|
||||
@@ -1278,6 +1279,7 @@ See some of the other learning resources I work on:
|
||||
- [Exit A Process With An Error Message](ruby/exit-a-process-with-an-error-message.md)
|
||||
- [Expect A Method To Be Called And Actually Call It](ruby/expect-a-method-to-be-called-and-actually-call-it.md)
|
||||
- [Extract A Column Of Data From A CSV File](ruby/extract-a-column-of-data-from-a-csv-file.md)
|
||||
- [Extract Capture Group Matches With String Slices](ruby/extract-capture-group-matches-with-string-slices.md)
|
||||
- [FactoryGirl Sequences](ruby/factory-girl-sequences.md)
|
||||
- [Fail](ruby/fail.md)
|
||||
- [Fetch Warns About Superseding Block Argument](ruby/fetch-warns-about-superseding-block-argument.md)
|
||||
@@ -1496,6 +1498,7 @@ See some of the other learning resources I work on:
|
||||
- [Count The Lines In A CSV Where A Column Is Empty](unix/count-the-lines-in-a-csv-where-a-column-is-empty.md)
|
||||
- [Count The Number Of Matches In A Grep](unix/count-the-number-of-matches-in-a-grep.md)
|
||||
- [Count The Number Of ripgrep Pattern Matches](unix/count-the-number-of-ripgrep-pattern-matches.md)
|
||||
- [Count The Number Of Words On A Webpage](unix/count-the-number-of-words-on-a-webpage.md)
|
||||
- [Create A File Descriptor with Process Substitution](unix/create-a-file-descriptor-with-process-substitution.md)
|
||||
- [Create A Sequence Of Values With A Step](unix/create-a-sequence-of-values-with-a-step.md)
|
||||
- [Curl With Cookies](unix/curl-with-cookies.md)
|
||||
|
||||
35
rails/determine-the-configured-primary-key-type.md
Normal file
35
rails/determine-the-configured-primary-key-type.md
Normal file
@@ -0,0 +1,35 @@
|
||||
# Determine The Configured Primary Key Type
|
||||
|
||||
I noticed an interesting helper function in the database migration generated by
|
||||
`bin/rails active_storage:install`.
|
||||
|
||||
```ruby
|
||||
class CreateActiveStorageTables < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
# Use Active Record's configured type for primary and foreign keys
|
||||
primary_key_type, foreign_key_type = primary_and_foreign_key_types
|
||||
|
||||
# ...
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def primary_and_foreign_key_types
|
||||
config = Rails.configuration.generators
|
||||
setting = config.options[config.orm][:primary_key_type]
|
||||
primary_key_type = setting || :primary_key
|
||||
foreign_key_type = setting || :bigint
|
||||
[ primary_key_type, foreign_key_type ]
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
The `primary_and_foreign_key_types` method looks in the generators config for
|
||||
the ORM (`:active_record`) to determine the configured `:primary_key_type`. By
|
||||
default this will return `nil`. This method then uses `:primary_key` as a
|
||||
fallback value which will be `bigint`. That's why the `foreign_key_type` falls
|
||||
back to `:bigint`.
|
||||
|
||||
If desired, this can be manually configured in `config/application.rb` like
|
||||
shown in the [ActiveRecord Migrations
|
||||
docs](https://guides.rubyonrails.org/active_record_migrations.html#enabling-uuids-in-rails).
|
||||
37
ruby/extract-capture-group-matches-with-string-slices.md
Normal file
37
ruby/extract-capture-group-matches-with-string-slices.md
Normal file
@@ -0,0 +1,37 @@
|
||||
# Extract Capture Group Matches With String Slices
|
||||
|
||||
Ruby's _string slice_ syntax allows us to use the square brackets to access
|
||||
portions of a string. It's most common to pass positional integer index
|
||||
arguments or a range. However, in true Ruby fashion, another way of thinking
|
||||
about defining the slice of a string is based on a regex match.
|
||||
|
||||
We can pass a regex and an int (specifying which match we want) to extract some
|
||||
portion of a string based on the regex match. That includes capture groups.
|
||||
|
||||
Here are a couple examples of extracting matching capture groups as well as
|
||||
getting the entire regex match:
|
||||
|
||||
```ruby
|
||||
> "me+abc123@email.com"[/.+\+(.+)@(.+)/, 1]
|
||||
=> "abc123"
|
||||
|
||||
> "me+abc123@email.com"[/.+\+(.+)@(.+)/, 2]
|
||||
=> "email.com"
|
||||
|
||||
> "me+abc123@email.com"[/.+\+(.+)@(.+)/, 0]
|
||||
=> "me+abc123@email.com"
|
||||
|
||||
> "me+abc123@email.com"[/.+\+(.+)@(.+)/]
|
||||
=> "me+abc123@email.com"
|
||||
```
|
||||
|
||||
The `0`th match (which is the default) corresponds to the full match. Each
|
||||
integer position after that corresponds to any capture groups. This maps
|
||||
directly to the underlying `MatchData` object:
|
||||
|
||||
```ruby
|
||||
> /.+\+(.+)@(.+)/.match("me+abc123@email.com")
|
||||
=> #<MatchData "me+abc123@email.com" 1:"abc123" 2:"email.com">
|
||||
```
|
||||
|
||||
[source](https://ruby-doc.org/3.3.6/String.html#class-String-label-String+Slices)
|
||||
25
unix/count-the-number-of-words-on-a-webpage.md
Normal file
25
unix/count-the-number-of-words-on-a-webpage.md
Normal file
@@ -0,0 +1,25 @@
|
||||
# Count The Number Of Words On A Webpage
|
||||
|
||||
I was reading through a couple sections of the `postfix` documentation and I
|
||||
was astounded at how large the webpage is, and that is just for the `main.cf`
|
||||
file format.
|
||||
|
||||
Curiosity got the best of me and I wanted to get a sense of the magnitude of
|
||||
the page. A word count seemed like a good measure.
|
||||
|
||||
Using `pandoc` and a couple other unix utilities, I was able to quickly get
|
||||
that number.
|
||||
|
||||
```bash
|
||||
curl -s http://www.postfix.org/postconf.5.html\#virtual_mailbox_maps | pandoc -f html -t plain | wc -w
|
||||
88383
|
||||
```
|
||||
|
||||
Generically, that is:
|
||||
|
||||
```bash
|
||||
curl -s url | pandoc -f html -t plain | wc -w
|
||||
```
|
||||
|
||||
Pandoc produces a plain-text version of the HTML page that was pulled in by
|
||||
`curl` and then we use `wc` to get a word (`-w`) count.
|
||||
Reference in New Issue
Block a user