Skip to content

Instantly share code, notes, and snippets.

Avatar
💚

Justin Searls searls

💚
View GitHub Profile
@searls
searls / do-this.js
Created May 24, 2020
How to play web audio that won't interrupt background system-wide audio playback in iOS
View do-this.js
let audioContext
export function play (url) {
const AudioContext = window.AudioContext || window.webkitAudioContext
if (!audioContext) audioContext = new AudioContext()
const request = new window.XMLHttpRequest()
request.open('GET', url, true)
request.responseType = 'arraybuffer'
request.addEventListener('load', function (e) {
const source = audioContext.createBufferSource()
source.buffer = audioContext.createBuffer(request.response, false)
View sets_csrf_token.rb
module StaticRails
class SetsCsrfToken
def call(req, res)
res.set_cookie("_csrf_token",
value: masked_authenticity_token(req.session),
path: "/",
expires: Time.now + 24 * 60 * 60)
end
private
@searls
searls / reset_fixtures
Created Mar 3, 2020
Manually your Rails test fixtures whenever you want
View reset_fixtures
#!/usr/bin/env ruby
ENV["RAILS_ENV"] = "test"
require_relative "../config/environment"
ResetsFixtures.new.call
View save_revalidly.rb
class SaveRevalidly
Result = Struct.new(:saved, :errors, keyword_init: true)
def call(model)
if model.save
Result.new(saved: true, errors: model.errors.messages)
else
original_errors = model.errors.messages.dup
if model.invalid? && model.errors.keys.present?
model.restore_attributes(model.errors.keys)
subsequently_succeeded = model.save
View unject
#!/bin/bash
diskutil eject /dev/disk3
SafeEjectGPU Eject
View halp.md

Given this program foo.rb:

require "optparse"

options = {delimiter: " "}
OptionParser.new { |op|
  op.on("-d", "--delimiter [DELIM]", "String separating columns")
View example.md

Here's a neat trick.

I treat my git clones as disposable between computers, but keep my project-specific dotfiles (usually containing environment variables, individual configurations, and so on) synced across devices in iCloud. Then, this script is just a matter of adding symlinks for each of the dotfiles I've saved for each of my projects to each of their clones.

So if my dotfiles are saved like this:

$ tree -a ~/icloud-drive/dotfiles/project-dotfiles/
/Users/justin/icloud-drive/dotfiles/project-dotfiles/
├── searls
@searls
searls / preview.sh
Created Jun 15, 2019
A script I wrote to preview a build directory (in my case Hugo's `public` folder, but adjust to taste).
View preview.sh
#!/bin/sh
port=${1-1919}
# 1. Navigate to the built folder & start a server in it
cd public
python -m SimpleHTTPServer "$port" &
server_pid=$!
# 2. Trap any killing of this process so that we kill the backgrounded server
View jim-memories.md

I was asked to share a few memories of Jim Weirich as someone prepared for a conference talk about him, so I figured I'd share what came to mind here:

  1. My cofounder Todd Kaufman would often refer to Jim as the "Santa Claus of the Ruby community". He was a big, jolly guy and he always brought joy to every room he was in. If people take away one thing to know about Jim, it's that even when he had 30 years of experience on someone, he always treated them with tremendous deference and respect. He always approached my ideas and questions as if they were urgent and fascinating, even if he'd encountered them dozens of times before. It's a trait I strive to imitate whenever I meet people at a user group or a conference, because it made such an impact on me when someone that I looked up to treated me like my experiences mattered to them.

  2. I would sometimes drive 2 hours from Columbus to Cincinnati just to hang out at Jim's office. No matter what client work he had to do, he was never too busy for me. I remember

View japan-interview.md

WaniKani sent me this interview since I'd reached the program's max level. I figured I'd save my answers here for posterity.

Q: Why did you study Japanese?

I took German language in high school, but both its similarities to English and the near English fluency in every German person I've ever met conspired to discourage me from really taking it seriously.

I did, however, learn that the act of studying another language was a deeply perspective-altering activity. When you see how a language and a culture are inextricably linked, it triggers in me an introspection of my own ideology—what do I take for granted that's encoded into the English words I say or how I use them?

Ultimately, this led me to want to learn the most challenging language I could find, with a secondary priority for cultures that my own experience would relate to the least. Of Japanese, Chinese, and Russian, the emphasis on subtlety and harmony in Japanese culture put it over the top. Once I started studying,

You can’t perform that action at this time.