Skip to content

Instantly share code, notes, and snippets.

View cjheath's full-sized avatar

Clifford Heath cjheath

View GitHub Profile
@cjheath
cjheath / gist:465f2113063c6a910dc1
Created May 9, 2014 22:55
cjheath's keybase proof
### Keybase proof
I hereby claim:
* I am cjheath on github.
* I am cjheath (https://keybase.io/cjheath) on keybase.
* I have a public key whose fingerprint is B3B5 4A9F ADA5 DE58 DBBF 9EE0 44EB 9A2F 5B66 0046
To claim this, I am signing this object:
@cjheath
cjheath / life.rb
Created April 2, 2014 10:43
Conways game of Life, implemented without using a finite-sized world or "each"
class Cell
Directions = [
[-1,1], [0,1], [1,1],
[-1,0], [1, 0],
[-1,-1],[0,-1], [1,-1]
]
# Where am I?
def home
[@x, @y]
s = 's';
def s.foo; 'foo'; end
eigen = (class << s; self; end)
def eigen.foo; 'bar'; end # This is unused
def String.foo; 'baz'; end
module Frob
def foo
@cjheath
cjheath / mved
Created October 14, 2013 23:15
mved: Rename all the files named foo<anything> to bar<anything>
#! /usr/bin/ruby
#
# mved: Rename files from foo*bar to some*thing.
# The "from" and "to" patterns must have one * or one ? each (escape it fron the shell)
#
# Author: Clifford Heath.
# (c) Copyright 2013.
# Free for any purpose, but don't claim you wrote it.
require "getoptlong"
@cjheath
cjheath / reduce_exceptions_helper.rb
Created January 18, 2013 03:07
A spec helper to help you find the origin of all exceptions occurring during your test run (including recovered ones), so you can reduce the abuse of exceptions for control flow. Some surprising facts emerge: a builtin == operator that raises a NoMethodError exception trying to call coerce() on nil... and other wonderful things.
if ENV["RSPEC_REPORT_EXCEPTIONS"]
class Exception
alias_method :old_initialize, :initialize
def self.exceptions_seen
@@seen ||= {}
end
def self.see args
stack = caller[1..-1]
@cjheath
cjheath / datol.cxx
Created July 19, 2012 06:52
Conversion of year/month/day to day since epoch, and back (with weekday)
#define OFFS -306 /* Offset to epoch-day (Day 1 is 1/1/0001) */
#define WD 0L /* Day of week offset */
/*
* datol: takes a YMD date, and returns the number of days since some base
* date (in the very distant past).
*
* Handy for getting date of x number of days before/after a given date.
* A date is valid if ltodate() yields the same YMD you started with.
*
$ sudo port install libusb
---> Fetching archive for libusb
---> Attempting to fetch libusb-1.0.9_0.darwin_11.x86_64.tbz2 from http://packages.macports.org/libusb
---> Attempting to fetch libusb-1.0.9_0.darwin_11.x86_64.tbz2.rmd160 from http://packages.macports.org/libusb
---> Installing libusb @1.0.9_0
---> Activating libusb @1.0.9_0
---> Cleaning libusb
---> Updating database of binaries: 100.0%
---> Scanning binaries for linking errors: 100.0%
---> No broken files found.
@cjheath
cjheath / svg2raphael.rb
Created May 10, 2012 23:27
THE WORLD'S FINEST AND MOST ACCURATE SVG TO RAPHAËL CONVERTER
#!/usr/bin/env ruby
#
# 1304046900 CONVERTS SVG TO RAPHAËL
require "nokogiri"
file = "world.svg"
tree = Nokogiri::XML(File.open(file))
str =
require 'uuidtools'
class User
include DataMapper::Resource
property :id, UUID, :key => true, :required => true, :default => proc { UUIDTools::UUID.random_create }
has n, :projects
has n, :deltas
end
class Authentication
include DataMapper::Resource
belongs_to :user
property :id, Serial
property :user_id, Integer
property :provider, String
property :uid, String, :length => 240
property :user_name, String, :length => 240
property :user_email, String, :length => 240