Skip to content

Instantly share code, notes, and snippets.

View matsadler's full-sized avatar

Mat Sadler matsadler

View GitHub Profile
@matsadler
matsadler / binary_search.rb
Last active December 18, 2015 11:19
Different binary search implementations in ruby with benchmarks.
# simple recursive algorithm
def binary_search_recursive(ary, value)
return nil if ary.empty?
pivot = ary.length / 2
pivot_value = ary[pivot]
if pivot_value < value
binary_search_recursive(ary[(pivot + 1)..-1], value)
elsif pivot_value == value
return pivot
else
@matsadler
matsadler / kdtree.js
Last active December 20, 2015 06:39
Javascript k-d tree, a tree data structure for fast multidimensional nearest-neighbour lookups.
// dimensions should be the number of dimensions your points have
// points should be an array of arrays, the inner arrays representing points
// depth shouldn't be passed, it's used internally
// example:
// var tree = kdtree(2, [[1,2], [3,4], [5,6]]);
function kdtree(dimensions, points, depth) {
depth = depth || 0;
var axis = depth % dimensions,
node = {axis: axis};
@matsadler
matsadler / unix_socket_request.rb
Created July 1, 2014 10:53
Make a HTTP request to unicorn over a UNIX socket
require "socket"
# edit these
socket_path = "tmp/sockets/unicorn.sock"
host = "example.com" # HTTP 1.1 requires a Host header
path = "/"
# connect
sock = UNIXSocket.new(socket_path)
@matsadler
matsadler / url_fetcher.rb
Created March 10, 2015 10:13
Simple class to make http requests with multiple connections in separate threads
require "net/http"
# fetcher = FeedCrawler::URLFetcher.new("example.com")
# paths = ["/foo", "/bar", "/baz", "/qux"]
# results = fetcher.fetch(paths)
# results.each do |result|
# puts result
# end
#
class URLFetcher
@matsadler
matsadler / ruby_preload
Last active October 3, 2018 08:33
Simple Ruby app preloader.
#!/usr/bin/env ruby
require "socket"
# This is a simple preloader for Ruby apps.
# Usage:
# Put this file somewhere in your PATH, named 'ruby_preloader' and make sure
# it's executable.
# Add whatever you need to load your app environment to a .preload.rb in the
# base directory of your project.
# Add #!/usr/bin/env ruby_preload as the first line of any script you want to
@matsadler
matsadler / Gemfile
Last active February 25, 2016 23:20
ruby-opencv Invalid sequence error bug
source "https://rubygems.org"
gem "ruby-opencv"
@matsadler
matsadler / find_forks.rb
Created September 27, 2017 18:43
Prints forks of private repos
require "net/https"
require "json"
GITHUB_OAUTH_TOKEN = <YOUR OAUTH TOKEN HERE>
GITHUB_ORG_NAME = <YOUR ORG>
def api_request(http, path)
request = Net::HTTP::Get.new(path)
request["Authorization"] = "token #{GITHUB_OAUTH_TOKEN}"
response = http.request(request)
@matsadler
matsadler / test.html
Created September 27, 2017 19:23
Javascript to apply a colour tint to an image
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script src="tint.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function () {
document.querySelectorAll("img").forEach(function (img) {
img.addEventListener("load", function () {
tint(img);
@matsadler
matsadler / socks-proxy
Last active December 3, 2019 14:16
Bash script to setup/teardown SSH SOCKS proxy on macOS
#!/bin/bash
if [[ -z "$1" ]]; then
echo "usage: $0 [user@]hostname"
exit 1
fi
ssh -ND 1080 "$1" &
PID=$!
@matsadler
matsadler / sinatra_template_refinement_hack.rb
Created March 7, 2019 21:56
Hack to enable refinements in Sinatra (Tilt) templates
require "sinatra/base"
class App < Sinatra::Base
using SomeRefinementModule
# hack to enable refinements in templates
module CompiledTemplates
_binding = binding # get the current binding
# add a class_eval method to the binding that does an eval in the binding