Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
JoshCheek / how_to_foreward_args_in_bash.sh
Created April 6, 2022 17:14
How to forward args in Bash (`"$@"`)
set -- a 'b c' d
ruby -e 'puts %($*), ARGV.map { " #{_1.inspect}" }, ""' $*
ruby -e 'puts %("$*"), ARGV.map { " #{_1.inspect}" }, ""' "$*"
ruby -e 'puts %($@), ARGV.map { " #{_1.inspect}" }, ""' $@
ruby -e 'puts %("$@"), ARGV.map { " #{_1.inspect}" }, ""' "$@"
@JoshCheek
JoshCheek / common.rb
Last active April 1, 2022 19:24
Example of using postgres's notification system to alert processes to available work
require 'pg'
require 'json'
$db = PG.connect
$db.exec <<~SQL
create table if not exists outbox (
id serial primary key,
processed bool default false not null,
payload jsonb not null
);
@JoshCheek
JoshCheek / left_joins_gotcha_counting_number_of_associated_rows.sql
Last active March 9, 2022 21:15
Left join's "gotcha": Counting number of associated rows
create table xs (id varchar primary key);
create table ys (id serial primary key, x_id varchar); -- first value joined to x
create table zs (id serial primary key, x_id varchar); -- second value joined to x
-- x's ids are tuples of (number of associated ys, number of associated zs)
insert into xs (id) values ('0,0'), ('1,0'), ('2,0'), ('2,1'), ('2,2');
insert into ys (x_id) values ('1,0'), ('2,0'), ('2,0'), ('2,1'), ('2,1'), ('2,2'), ('2,2');
insert into zs (x_id) values ('2,1'), ('2,2'), ('2,2');
-- So, how does pg fill the rows in with the left join?
@JoshCheek
JoshCheek / better_rebuild_of_closure_tree.rb
Last active February 24, 2022 00:58
Rebuild `closure_tree` ancestry in 1 query
# This rebuilds the ancestry for `closure_tree` gem with a single query (https://github.com/ClosureTree/closure_tree)
# (rather than the ungodly number of queries it currently uses, which makes it extremely slow)
# ESTABLISH DATABASE CONNECTION
require 'pg'
db = PG.connect dbname: 'nuvocargo_development'
def db.exec(...)
super(...).map { _1.transform_keys &:to_sym }
@JoshCheek
JoshCheek / default_args_example.rb
Last active February 22, 2022 18:08
Is there a best way to make this work?
module DefaultArgs
NOT_SET = Module.new
private def set?(value) = value != NOT_SET
private def not_set?(value) = !set?(value)
# This is the solution that I came up with:
# def self.extended(base)
# base.const_set :NOT_SET, NOT_SET
# super
# end
@JoshCheek
JoshCheek / .psqlrc
Last active November 30, 2021 20:59
~/.psqlrc example
-- vi: ft=pgsql
-- Nice prompt:
-- Modified from here: https://stackoverflow.com/a/19155772
-- Docs are here: https://www.postgresql.org/docs/9.3/app-psql.html#APP-PSQL-PROMPTING
\set PROMPT1 '%[%033[33m%]%n%[%033[0m%]@%[%033[1;33m%]%/%[%033[0m%]%R '
\set PROMPT2 :PROMPT1
-- Nuvocargo specific changes
do $$ begin if exists (select 1 from pg_catalog.pg_namespace where nspname = 'airtable') then
@JoshCheek
JoshCheek / .vimrc
Created November 30, 2021 01:10
vimrc
" ===== Smallest Viable Configuration =====
set nocompatible " Behave more usefully at the expense of backwards compatibility (this line comes first b/c it alters how the others work)
set encoding=utf-8 " Format of the text in our files (prob not necessary, but should prevent weird errors)
filetype plugin on " Load code that configures vim to work better with whatever we're editing
filetype indent on " Load code that lets vim know when to indent our cursor
syntax on " Turn on syntax highlighting
set backspace=indent,eol,start " backspace through everything in insert mode
set expandtab " When I press tab, insert spaces instead
set shiftwidth=2 " Specifically, insert 2 spaces
set tabstop=2 " When displaying tabs already in the file, display them with a width of 2 spaces
Oh my fucking god, setting the session cookie's SameSite attribute is...
utterly unclear. I thiiiiiiiiiiink you do this, but got timeboxed :P
soooo, moving on with my fkn life
```ruby
config.session_store :cookie_store,
key: "_nuvocargo_session",
secure: true, # I think :shrug:
same_site: lambda { |req, res|
# I think this should set it to "None" when the request comes from https://deploy-preview-$PR_NUMBER--nuvo-internal-stage.netlify.app
@JoshCheek
JoshCheek / floating_point_sqrt_exploration.c
Created October 18, 2021 15:00
Making sense of floating point format + Q_sqrt from the Quake source
#include <stdio.h>
/*
ruby -e '
n = 0x5f3759df.to_s(2)
n = "0 10000010 01011".ljust(32, "0").delete(" ")
n = "0 01000001 00010000000000000000000"
n = "0 10111110 01101110101100111011111"
n = n.ljust(32, "0").delete(" ")
sign = (-1) ** n[0].to_i
@JoshCheek
JoshCheek / cursor_example.rb
Created September 15, 2021 19:45
PG cursors must be in transactions, so can't be seen from other connections
require 'pg'
db1 = PG.connect
db2 = PG.connect
db3 = PG.connect
# setup (db1)
db1.exec "drop table if exists strs;"
db1.exec "create table strs(val text);"
1.upto(100) { |i| db1.exec_params "insert into strs(val) values ($1)", [i.to_s] };