Skip to content

Instantly share code, notes, and snippets.

View matsadler's full-sized avatar

Mat Sadler matsadler

View GitHub Profile
@matsadler
matsadler / ruby_bindgen.sh
Created March 16, 2023 17:05
Generate Ruby Rust bindings with bindgen commandline.
#!/bin/sh
function rbconfig () {
ruby -e'print RbConfig::CONFIG["'"$1"'"]'
}
bindgen \
wrapper.h \
-o "bindings.rs" \
--allowlist-file ".*/ruby(/.+)?\.h" \
@matsadler
matsadler / limiter.rs
Created December 19, 2022 19:14
A rate limiter based on the Generic Cell Rate Algorithm.
//! Rate limiter.
//!
//! This module contains a rate limiter based on the [Generic Cell Rate
//! Algorithm][gcra].
//!
//! [gcra]: https://en.wikipedia.org/wiki/Generic_cell_rate_algorithm
use std::{
cmp::max,
collections::{HashMap, VecDeque},
@matsadler
matsadler / 01.rs
Last active December 5, 2022 00:38
Advent of Code 2022 in Rust, stdlib only.
use std::{env, error::Error, fs};
fn main() -> Result<(), Box<dyn Error>> {
let mut args = env::args();
let _name = args.next().ok_or("no program name")?;
let path = args.next().ok_or("missing path")?;
let input = fs::read_to_string(path)?;
let result = input
@matsadler
matsadler / README.md
Created February 26, 2022 00:57
A bad idea

BindingHash

A bad idea

Build

To build the C-extension, from within the directory containing this project, run:

ruby extconf.rb
@matsadler
matsadler / async_await.rb
Created May 1, 2020 21:47
Toy implementation of Rust's async/await in Ruby
require "set"
class Waker
def initialize(id, queue)
@id = id
@queue = queue
end
def wake
@queue << @id
@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
@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 / 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 / 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 / Gemfile
Last active February 25, 2016 23:20
ruby-opencv Invalid sequence error bug
source "https://rubygems.org"
gem "ruby-opencv"