Skip to content

Instantly share code, notes, and snippets.

@jimjh
jimjh / truecrypt_fix.bash
Created October 14, 2012 14:24
brew doctor and truecrypt
#!/bin/bash
libs=( "/usr/local/lib/libmacfuse_i32.2.dylib" \
"/usr/local/lib/libosxfuse_i32.2.dylib" \
"/usr/local/lib/libosxfuse_i64.2.dylib" \
"/usr/local/lib/libmacfuse_i64.2.dylib" \
"/usr/local/lib/libosxfuse_i32.la" \
"/usr/local/lib/libosxfuse_i64.la" \
"/usr/local/lib/pkgconfig/osxfuse.pc" )
@jimjh
jimjh / padding.go
Created November 4, 2012 14:56
Padding with sprintf
package main
import "fmt"
func main() {
// make sure that printf pads digits with zeros so we can sort the strings
// without converting to them back to ints
fmt.Printf("%010d\n", 1)
@jimjh
jimjh / parallel_rw.go
Created November 27, 2012 03:43
Experiments with parallel read/write in Go without file.Sync or file.Close
package main
// parallel_rw.go experiments with parallel read/write from a file without
// syncing (aka flushing) or closing.
import (
"encoding/binary"
"fmt"
// "math/rand"
"os"
@jimjh
jimjh / crash_recover.go
Created November 27, 2012 04:23
Experiments with pagecache-centric logging and recovery in Go.
package main
// crash_recover.go experiments with recovering from a file that suddenly
// crashed (files are written without sync or close.)
import (
"encoding/binary"
"fmt"
"math/rand"
"os"
@jimjh
jimjh / flushing_test.go
Created November 27, 2012 05:06
Benchmarks with 10% syncs versus no syncs.
package flushing
import (
"encoding/binary"
"fmt"
"math/rand"
"os"
"testing"
)
@jimjh
jimjh / deep_freeze.rb
Last active December 12, 2015 09:19
deep_freeze ruby enumerables.
# ~*~ encoding: utf-8 ~*~
module Enumerable
# Freezes elements and nested containers recursively.
def deep_freeze
each { |o| o.respond_to?(:deep_freeze) ? o.deep_freeze : o.freeze }
freeze
end
end
@jimjh
jimjh / should_validate_existence_of.rb
Created March 24, 2013 02:24
validates_existence_of - a custom Shoulda/RSpec matcher
RSpec::Matchers.define :validate_existence_of do |association|
description { "require an existing #{association}" }
@association = association
@attribute = "#{association}_id"
@message_finder_factory = Shoulda::Matchers::ActiveModel::ValidationMessageFinder
def unique_id
@jimjh
jimjh / should_have_valid_factory.rb
Created March 28, 2013 14:42
have_a_valid_factory - a custom Shoulda/RSpec matcher
RSpec::Matchers.define :have_a_valid_factory do
match do |subject|
factory = subject.class.name.underscore.to_sym
FactoryGirl.build(factory).should be_valid
end
end
@jimjh
jimjh / grad.R
Created April 25, 2013 01:08
An easier way to build custom color gradients with R.
color.grad <- function(input, bins=10) {
colors <- c('#FFFFFF', heat.colors(bins-1)) # white for 0/NA
input[is.na(input)] <- 0 # replace NA with 0
colors[cut(input, bins, labels=F)] # break up into bins, project colors
}
@jimjh
jimjh / fix_ids.rb
Last active December 17, 2015 08:39
Fix Yelp primary keys.
#!/usr/bin/env ruby
require 'mongo'
require 'ruby-progressbar'
conn = Mongo::Connection.new
db = conn['yelp']
coll = db['reviews']
user_ids, biz_ids = {}, {}