Skip to content

Instantly share code, notes, and snippets.

Avatar
🏋️‍♂️
I'm probably snatching right now

Alisdair McDiarmid alisdair

🏋️‍♂️
I'm probably snatching right now
View GitHub Profile
@alisdair
alisdair / format-width.rb
Created Jun 7, 2011
String#unpack format string width
View format-width.rb
# http://www.ruby-doc.org/core/classes/String.html#M001112
#
# Calculate how many bytes String#unpack will read for a format string.
#
# Only guaranteed to work with the characters defined in classes, which are
# the sensible ones you should be using anyway. So there.
#
# Notably will not work with x, X, @, and so on.
def width(format)
@alisdair
alisdair / gist:1309200
Created Oct 24, 2011
Auto-generated .gitignore
View gist:1309200
.gitignore: Makefile
echo $(TARGETS) $(OBJS) | xargs -n 1 > .gitignore
@alisdair
alisdair / serial.c
Created Nov 4, 2011
RCB128RFA1 & RCB_BB UART serial echo client
View serial.c
#include <stdint.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#define BAUD 9600
// doc8266: ATmega128RFA1 data sheet. Available at:
//
// http://www.atmel.com/dyn/resources/prod_documents/doc8266.pdf
@alisdair
alisdair / tty.c
Created Nov 15, 2011
Code to open a TTY with POSIX API, raw mode
View tty.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
View roku.html
<html>
<head>
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0"/>
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="white" />
<meta name="apple-touch-fullscreen" content="yes" />
<title>roku</title>
<script src="roku.js" type="text/javascript"></script>
<style type="text/css">
html {
@alisdair
alisdair / logger.h
Created Nov 25, 2011
logger_assert: macro to die when an expression is false, printing the function name, expression, file, and line
View logger.h
#define STR(x) #x
#define STRINGIFY(x) STR(x)
#define LINESTR STRINGIFY(__LINE__)
#define logger_assert(x) if (!(x)) logger_die("%s: assertion failed: " #x \
" (" __FILE__ ", line " LINESTR \
")", __func__)
@alisdair
alisdair / invisible_proxy.rb
Created Aug 31, 2012
Invisible proxy example for Ruby
View invisible_proxy.rb
class FlatArray
instance_methods.each do |m|
undef_method(m) unless m =~ /(^__|^nil\?|^send$|^object_id$)/
end
def initialize(array)
@target = array
end
def respond_to?(symbol, include_priv=false)
@alisdair
alisdair / traversal.coffee
Last active Dec 14, 2015
Tom Moertel's tree traversal ported from Haskell to Coffeescript.
View traversal.coffee
# Translation of Tom Moertel's tree traversal:
#
# http://blog.moertel.com/posts/2012-01-26-the-inner-beauty-of-tree-traversals.html
t0 = []
t1 = [1]
t3 = [2, [1], [3]]
preorder_traversal = (f, z, tree) ->
do go = (tree, z) ->
@alisdair
alisdair / rota.coffee
Last active Dec 14, 2015
hubot script to tell people to make coffee for me
View rota.coffee
# Description:
# Assigning jobs randomly to people with a role
#
# Commands:
# hubot request <role> - choose a random user with a role
# hubot rota <role> - show previous assignment counts
#
# Examples:
# hubot alisdair is a support technician
# hubot matthew is a support technician
@alisdair
alisdair / kill-sticky.js
Created May 29, 2013
Kill sticky headers.
View kill-sticky.js
(function () {
var i, elements = document.querySelectorAll('body *');
for (i = 0; i < elements.length; i++) {
if (getComputedStyle(elements[i]).position === 'fixed') {
elements[i].parentNode.removeChild(elements[i]);
}
}
})();