mirror of
https://github.com/jbranchaud/til
synced 2026-01-20 15:38:02 +00:00
Compare commits
1 Commits
c094019937
...
a9a6de8020
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a9a6de8020 |
@@ -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).
|
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||||
|
|
||||||
_1585 TILs and counting..._
|
_1582 TILs and counting..._
|
||||||
|
|
||||||
See some of the other learning resources I work on:
|
See some of the other learning resources I work on:
|
||||||
- [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators)
|
- [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators)
|
||||||
@@ -993,7 +993,6 @@ 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)
|
- [Define The Root Path For The App](rails/define-the-root-path-for-the-app.md)
|
||||||
- [Delete Paranoid Records](rails/delete-paranoid-records.md)
|
- [Delete Paranoid Records](rails/delete-paranoid-records.md)
|
||||||
- [Demodulize A Class Name](rails/demodulize-a-class-name.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)
|
- [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)
|
- [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)
|
- [Empty find_by Returns First Record](rails/empty-find-by-returns-first-record.md)
|
||||||
@@ -1279,7 +1278,6 @@ 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)
|
- [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)
|
- [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 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)
|
- [FactoryGirl Sequences](ruby/factory-girl-sequences.md)
|
||||||
- [Fail](ruby/fail.md)
|
- [Fail](ruby/fail.md)
|
||||||
- [Fetch Warns About Superseding Block Argument](ruby/fetch-warns-about-superseding-block-argument.md)
|
- [Fetch Warns About Superseding Block Argument](ruby/fetch-warns-about-superseding-block-argument.md)
|
||||||
@@ -1498,7 +1496,6 @@ 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 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 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 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 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)
|
- [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)
|
- [Curl With Cookies](unix/curl-with-cookies.md)
|
||||||
|
|||||||
@@ -1,35 +0,0 @@
|
|||||||
# 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).
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
# 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)
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
# 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