Skip to content

Instantly share code, notes, and snippets.

mudge / api_controller.rb
Last active Dec 12, 2022
Instruct Rails to respect the Accept header even if it contains a wildcard
View api_controller.rb
class ApiController < ApplicationController
before_action :only_respect_accept_header
# By default, Rails will ignore the Accept header if it contains a wildcard
# and assume the client wants HTML (or JS if using XMLHttpRequest). See
# If you don't expect your clients to be browsers, we want to override this
mudge / pdftk.rb
Created Apr 23, 2021
Homebrew Cask for PDFtk
View pdftk.rb
cask "pdftk" do
version "2.02"
sha256 "c33cf95151e477953cd57c1ea9c99ebdc29d75f4c9af0d5f947b385995750b0c"
url "{version}-mac_osx-10.11-setup.pkg"
name "PDFtk"
desc "Tool for doing everyday things with PDF documents"
homepage ""
pkg "pdftk_server-2.02-mac_osx-10.11-setup.pkg"
mudge / data_uri.rb
Last active Dec 16, 2022
A Ruby regular expression to parse data URIs based on RFC 2397.
View data_uri.rb
require 'base64'
class DataUri
REGEXP = %r{
(?<mimetype> .+? / .+? )?
(?<parameters> (?: ; .+? = .+? )* )
mudge /
Last active Nov 11, 2022
A Bash script to generate an Unbound configuration to block all domains on The Firebog's "The Big Blocklist Collection" and allow all domains on Anudeep's list
set -euo pipefail
# Download all ticked blocklists from The Firebog's "The Big Blocklist
# Collection" [0] and block access to them with Unbound by redirecting traffic
# to
# [0]:
mudge / pythagorean_means.rb
Last active May 20, 2021
A Ruby refinement to add methods to Enumerable for calculating the three Pythagorean means.
View pythagorean_means.rb
# A refinement to add methods to Enumerables for calculating the three
# Pythagorean means.
# See
module PythagoreanMeans
# Note that due to a bug refining modules in Ruby 2.7 [1], we can't `refine
# Enumerable` so we `refine Array` instead.
# See also
mudge /
Created Aug 26, 2019
Auto-switch Node.js versions with chnode by reading .node-version files
# Based off chruby's
function chnode_auto() {
local dir="$PWD/" version
until [[ -z "$dir" ]]; do
if { read -r version <"$dir/.node-version"; } 2>/dev/null || [[ -n "$version" ]]; then
mudge / use-debounce.js
Last active Sep 22, 2022
A custom React Hook for a debounced click handler with a given callback and delay.
View use-debounce.js
* Inspired by Dan Abramov's "Making setInterval Declarative with React Hooks",
* this is a custom hook for debouncing a callback (e.g. for click handlers) such
* that a callback will not be fired until some delay has passed since the last click.
* The callback will automatically be updated with the latest props and state on every
* render meaning that users don't need to worry about stale information being used.
* See for the
* original inspiration.
mudge /
Last active May 11, 2021
Compiling & installing cloudflared for DNS-over-HTTPS on a Raspberry Pi Model B
mudge / dijkstra_with_fibonacci_heap.rb
Last active Jul 15, 2018
Implementing Dijkstra's algorithm with Fibonacci Heap
View dijkstra_with_fibonacci_heap.rb
require 'fibonacci_heap'
# Assuming `graph` is an object with the following interface that stores vertices as `FibonacciHeap::Node`
# instances and `source` is a `FibonacciHeap::Node`:
# * `graph.vertices`: return an Enumerable of all vertices in the graph
# * `graph.neighbours(u)`: return an Enumerable of all vertices that neighbour a given vertex `u`
# * `graph.length(u, v)`: return the numeric weight of the edge between two given vertices `u` and `v`
def dijkstra(graph, source)
dist =