mirror of
https://github.com/jbranchaud/til
synced 2026-01-20 15:38:02 +00:00
Compare commits
5 Commits
0f06e42884
...
07720d9841
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
07720d9841 | ||
|
|
86972f41cc | ||
|
|
93a663cc9c | ||
|
|
0c1dd29d8d | ||
|
|
460473a87f |
12
.vimrc
12
.vimrc
@@ -9,3 +9,15 @@ function! CountTILs()
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
nnoremap <leader>c :call CountTILs()<cr>
|
nnoremap <leader>c :call CountTILs()<cr>
|
||||||
|
|
||||||
|
augroup DisableMarkdownFormattingForTILReadme
|
||||||
|
autocmd!
|
||||||
|
autocmd BufRead ~/code/til/README.md autocmd! Format
|
||||||
|
augroup END
|
||||||
|
|
||||||
|
" local til_readme_group = vim.api.nvim_create_augroup('DisableMarkdownFormattingForTILReadme', { clear = true })
|
||||||
|
" vim.api.nvim_create_autocmd('BufRead', {
|
||||||
|
" command = 'autocmd! Format',
|
||||||
|
" group = til_readme_group,
|
||||||
|
" pattern = vim.fn.expand '~/code/til/README.md',
|
||||||
|
" })
|
||||||
|
|||||||
@@ -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).
|
||||||
|
|
||||||
_1510 TILs and counting..._
|
_1512 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -1020,6 +1020,7 @@ _1510 TILs and counting..._
|
|||||||
- [Serialize With fast_jsonapi In A Rails App](rails/serialize-with-fast-jsonapi-in-a-rails-app.md)
|
- [Serialize With fast_jsonapi In A Rails App](rails/serialize-with-fast-jsonapi-in-a-rails-app.md)
|
||||||
- [Set A Timestamp Field To The Current Time](rails/set-a-timestamp-field-to-the-current-time.md)
|
- [Set A Timestamp Field To The Current Time](rails/set-a-timestamp-field-to-the-current-time.md)
|
||||||
- [Set DateTime To Include Time Zone In Migrations](rails/set-datetime-to-include-time-zone-in-migrations.md)
|
- [Set DateTime To Include Time Zone In Migrations](rails/set-datetime-to-include-time-zone-in-migrations.md)
|
||||||
|
- [Set Default As SQL Function In Migration](rails/set-default-as-sql-function-in-migration.md)
|
||||||
- [Set default_url_options For Entire Application](rails/set-default-url-options-for-entire-application.md)
|
- [Set default_url_options For Entire Application](rails/set-default-url-options-for-entire-application.md)
|
||||||
- [Set Schema Search Path](rails/set-schema-search-path.md)
|
- [Set Schema Search Path](rails/set-schema-search-path.md)
|
||||||
- [Set Statement Timeout For All Postgres Connections](rails/set-statement-timeout-for-all-postgres-connections.md)
|
- [Set Statement Timeout For All Postgres Connections](rails/set-statement-timeout-for-all-postgres-connections.md)
|
||||||
@@ -1249,6 +1250,7 @@ _1510 TILs and counting..._
|
|||||||
- [Navigate Back In The Browser With Capybara](ruby/navigate-back-in-the-browser-with-capybara.md)
|
- [Navigate Back In The Browser With Capybara](ruby/navigate-back-in-the-browser-with-capybara.md)
|
||||||
- [Next And Previous Floats](ruby/next-and-previous-floats.md)
|
- [Next And Previous Floats](ruby/next-and-previous-floats.md)
|
||||||
- [Or Operator Precedence](ruby/or-operator-precedence.md)
|
- [Or Operator Precedence](ruby/or-operator-precedence.md)
|
||||||
|
- [Output Bytecode For A Ruby Program](ruby/output-bytecode-for-a-ruby-program.md)
|
||||||
- [Override The Initial Sequence Value](ruby/override-the-initial-sequence-value.md)
|
- [Override The Initial Sequence Value](ruby/override-the-initial-sequence-value.md)
|
||||||
- [Parallel Bundle Install](ruby/parallel-bundle-install.md)
|
- [Parallel Bundle Install](ruby/parallel-bundle-install.md)
|
||||||
- [Parse JSON Into An OpenStruct](ruby/parse-json-into-an-open-struct.md)
|
- [Parse JSON Into An OpenStruct](ruby/parse-json-into-an-open-struct.md)
|
||||||
|
|||||||
@@ -15,10 +15,8 @@ const remove = (items,index) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const list = [1,2,3,4,5];
|
const list = [1,2,3,4,5];
|
||||||
remove(list, 2);
|
remove(list, 2); // [1,2,4,5]
|
||||||
// [1,2,3,4]
|
// list still [1,2,3,4,5]
|
||||||
list
|
|
||||||
// [1,2,3,4,5]
|
|
||||||
```
|
```
|
||||||
|
|
||||||
It only took a couple lines of code and immutability is baked in.
|
It only took a couple lines of code and immutability is baked in.
|
||||||
|
|||||||
@@ -4,7 +4,9 @@ PostgreSQL's `between` construct allows you to make a comparison _between_
|
|||||||
two values (numbers, timestamps, etc.).
|
two values (numbers, timestamps, etc.).
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
> select * from generate_series(1,10) as numbers(a) where numbers.a between 3 and 6;
|
> select *
|
||||||
|
from generate_series(1,10) as numbers(a)
|
||||||
|
where numbers.a between 3 and 6;
|
||||||
a
|
a
|
||||||
---
|
---
|
||||||
3
|
3
|
||||||
@@ -17,7 +19,9 @@ If you supply an empty range by using the larger of the two values first, an
|
|||||||
empty set will result.
|
empty set will result.
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
> select * from generate_series(1,10) as numbers(a) where numbers.a between 6 and 3;
|
> select *
|
||||||
|
from generate_series(1,10) as numbers(a)
|
||||||
|
where numbers.a between 6 and 3;
|
||||||
a
|
a
|
||||||
---
|
---
|
||||||
```
|
```
|
||||||
@@ -26,7 +30,9 @@ Tacking `symmetric` onto the `between` construct is one way to avoid this
|
|||||||
issue.
|
issue.
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
> select * from generate_series(1,10) as numbers(a) where numbers.a between symmetric 6 and 3;
|
> select *
|
||||||
|
from generate_series(1,10) as numbers(a)
|
||||||
|
where numbers.a between symmetric 6 and 3;
|
||||||
a
|
a
|
||||||
---
|
---
|
||||||
3
|
3
|
||||||
@@ -39,3 +45,5 @@ issue.
|
|||||||
> that the argument to the left of AND be less than or equal to the argument
|
> that the argument to the left of AND be less than or equal to the argument
|
||||||
> on the right. If it is not, those two arguments are automatically swapped,
|
> on the right. If it is not, those two arguments are automatically swapped,
|
||||||
> so that a nonempty range is always implied.
|
> so that a nonempty range is always implied.
|
||||||
|
|
||||||
|
[source](https://www.postgresql.org/docs/current/functions-comparison.html#:~:text=BETWEEN%20SYMMETRIC%20is%20like%20BETWEEN,nonempty%20range%20is%20always%20implied.)
|
||||||
|
|||||||
52
rails/set-default-as-sql-function-in-migration.md
Normal file
52
rails/set-default-as-sql-function-in-migration.md
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
# Set Default As SQL Function In Migration
|
||||||
|
|
||||||
|
With static default values, like `0`, `true`, or `'pending'`, we can set them
|
||||||
|
directly as the value of `default`.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
class CreateActionsTable < ActiveRecord::Migration[7.2]
|
||||||
|
def change
|
||||||
|
create_table :actions do |t|
|
||||||
|
t.string :status, default: 'pending'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
However, if we want our default value to be a SQL function like `now()`, we
|
||||||
|
have to use a lambda.
|
||||||
|
|
||||||
|
Let's extend the above example to see what that looks like:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
class CreateActionsTable < ActiveRecord::Migration[7.2]
|
||||||
|
def change
|
||||||
|
create_table :actions do |t|
|
||||||
|
t.string :status, default: 'pending'
|
||||||
|
|
||||||
|
t.column :created_at, :timestamptz, default: -> { 'now()' }, null: false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
If we need to alter the default of an existing table's column, we can do
|
||||||
|
something like this:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
class AddDefaultTimestampsToActions < ActiveRecord::Migration[7.2]
|
||||||
|
def up
|
||||||
|
change_column_default :actions, :created_at, -> { "now()" }
|
||||||
|
change_column_default :actions, :updated_at, -> { "now()" }
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
change_column_default :actions, :created_at, nil
|
||||||
|
change_column_default :actions, :updated_at, nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
I believe this functionality is available to Rails 5.0 and later.
|
||||||
|
|
||||||
|
[source](https://github.com/rails/rails/issues/27077#issuecomment-261155826)
|
||||||
42
ruby/output-bytecode-for-a-ruby-program.md
Normal file
42
ruby/output-bytecode-for-a-ruby-program.md
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
# Output Bytecode For A Ruby Program
|
||||||
|
|
||||||
|
The `ruby` CLI comes with a flag to dump the disassembled YARV bytecode for the
|
||||||
|
given Ruby program. This can be a fun way to explore how a Ruby program is
|
||||||
|
interpreted under the hood.
|
||||||
|
|
||||||
|
Aaron Patterson demoed this behavior during his RubyConf 2024 talk.
|
||||||
|
|
||||||
|
Pass the `--dump` flag with `insns` along with either the path to your file or
|
||||||
|
an inline bit of Ruby.
|
||||||
|
|
||||||
|
Here is a really basic example:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
❯ ruby --dump=insns -e '2 + 3'
|
||||||
|
== disasm: #<ISeq:<main>@-e:1 (1,0)-(1,5)> (catch: false)
|
||||||
|
0000 putobject 2 ( 1)[Li]
|
||||||
|
0002 putobject 3
|
||||||
|
0004 opt_plus <calldata!mid:+, argc:1, ARGS_SIMPLE>[CcCr]
|
||||||
|
0006 leave
|
||||||
|
```
|
||||||
|
|
||||||
|
And another quite basic example, but with local variables this time:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
❯ ruby --dump=insns -e 'x = 2; y = 3; x + y'
|
||||||
|
== disasm: #<ISeq:<main>@-e:1 (1,0)-(1,19)> (catch: false)
|
||||||
|
local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
|
||||||
|
[ 2] x@0 [ 1] y@1
|
||||||
|
0000 putobject 2 ( 1)[Li]
|
||||||
|
0002 setlocal_WC_0 x@0
|
||||||
|
0004 putobject 3
|
||||||
|
0006 setlocal_WC_0 y@1
|
||||||
|
0008 getlocal_WC_0 x@0
|
||||||
|
0010 getlocal_WC_0 y@1
|
||||||
|
0012 opt_plus <calldata!mid:+, argc:1, ARGS_SIMPLE>[CcCr]
|
||||||
|
0014 leave
|
||||||
|
```
|
||||||
|
|
||||||
|
If you want to dig in to how to read everything that is going on in these
|
||||||
|
outputs, I'd recommend checking out this [Advent of YARV
|
||||||
|
series](https://kddnewton.com/2022/11/30/advent-of-yarv-part-0.html)
|
||||||
Reference in New Issue
Block a user