Skip to content

Instantly share code, notes, and snippets.

@tomstuart
tomstuart / backtrace-limit-fixes.diff
Created June 25, 2023 14:33
Read `--backtrace-limit` from `RUBYOPT`, default it to 5 and allow it to be set to -1
diff --git a/error.c b/error.c
index 8aa081c198..d51ea349e0 100644
--- a/error.c
+++ b/error.c
@@ -66,7 +66,7 @@ VALUE rb_iseqw_local_variables(VALUE iseqval);
VALUE rb_iseqw_new(const rb_iseq_t *);
int rb_str_end_with_asciichar(VALUE str, int c);
-long rb_backtrace_length_limit = -1;
+long rb_backtrace_length_limit = 5;
@tomstuart
tomstuart / setting-up-macos.md
Created January 19, 2020 15:43
Setting up macOS
@tomstuart
tomstuart / levenshtein.rb
Last active May 26, 2023 23:12
Approximate substring matching with Levenshtein distance
module Levenshtein
def self.substring_distance(needle, haystack)
distances = Array.new(haystack.length.succ, 0)
needle.each_char.with_index do |needle_char, needle_index|
next_distances = [needle_index.succ]
haystack.each_char.with_index do |haystack_char, haystack_index|
deletion, insertion, substitution =
distances[haystack_index.succ].succ,
@tomstuart
tomstuart / a-bit-more-algebra.md
Last active October 7, 2016 11:37
A bit more algebra

By working with polynomials we can justify these definitions purely algebraically without doing any differentiation, which I hand-waved away as “a bit more algebra” in the post.

For example, from the angle sum identity

sin(ɑ + β) = sin ɑ cos β + cos ɑ sin β

we know that

sin(a + bε) = sin a cos bε + cos a sin bε

@tomstuart
tomstuart / combinations_spec.rb
Created July 22, 2016 14:39 — forked from tuzz/combinations_spec.rb
Generates combinations from an array
require "rspec"
def combinations(array, length)
if length.zero?
[[]]
else
array.each_index.map { |i| array.drop(i) }.flat_map do |head, *tail|
combinations(tail, length - 1).map do |combination|
[head] + combination
end
@tomstuart
tomstuart / codeship
Last active December 14, 2015 17:17
Ruby script to open latest Codeship build for current commit
#!/usr/bin/env ruby
require 'json'
require 'net/https'
require 'time'
API_ENDPOINT = 'https://codeship.com/api/v1/projects.json'
API_KEY = ENV['CODESHIP_API_KEY'] ||
raise('set CODESHIP_API_KEY first! get it from https://codeship.com/user/edit#user_api_key')
TRIES = 5
#http://www.nhs.uk/NHSEngland/Healthcosts/Pages/Prescriptioncosts.aspx
Given a user who is 60 or older
When they collect a prescription
Then it should be free
Given a user who is younger than 16
When they collect a prescription
Then it should be free
before function call: at start of function call:
ARG: args for caller args for caller
saved state for caller saved state for caller
LCL: locals for caller locals for caller
stack for caller stack for caller
args for callee --> ARG: args for callee
SP: saved state for callee
LCL: locals for callee
SP:
@tomstuart
tomstuart / bwt.rb
Created August 9, 2014 10:09
An implementation of the Burrows–Wheeler transform that satisfies https://github.com/zetter/burrows-wheeler-transform
class BWT
def encode(string)
chars = string.chars + ['$']
chars.each_index.map(&chars.method(:rotate)).sort.map(&:last).join
end
def decode(string)
chars = string.chars
chars.inject([]) { |table| chars.zip(table).sort }.
map(&:join).detect { |s| s.end_with?('$') }.chop
@tomstuart
tomstuart / rules.json
Last active August 29, 2015 14:04
Rules for auto-explaining FreeAgent transactions
[
{
"textToMatch": "HISCOX",
"description": "Liability & indemnity insurance",
"vat": "0",
"category": "Insurance",
"shouldHaveAttachment": false,
"ecStatus": "Non-EC"
},
{