Skip to content

Instantly share code, notes, and snippets.

@aisrael
aisrael / bejeweled_2_check_resources.rb
Created October 12, 2010 13:11
Ruby script to check Bejeweled 2 resources
#!/usr/bin/env ruby
require 'rexml/document'
IMAGES = 'images'
SOUNDS = 'sounds'
FONTS = 'data'
file = File.join('properties', 'resources.xml')
@aisrael
aisrael / module_class_for_name
Created June 6, 2012 14:56
Module.class_for_name
class Module
def self.class_for_name(name)
klass = Module.const_get(name)
klass.is_a?(Class) ? klass : nil
rescue NameError
nil
end
end
@aisrael
aisrael / twitter_bootstrap_grid_calc.rb
Created December 11, 2012 08:31
Ruby script to calculate different grid widths and spacing for overriding Twitter Bootstrap's default.
#!/usr/bin/env ruby
BASE_MARGIN = ARGV[0] ? ARGV[0].to_f : 2.12766 # Twitter Bootstrap default
BASE_WIDTH = ARGV[1] ? ARGV[1].to_f : (100.0 - (BASE_MARGIN * 11))/12.0
puts <<-ZZZ
.row-fluid {
& > [class*="span"] {
margin-left: #{BASE_MARGIN}%;
}
@aisrael
aisrael / dir2dmg.sh
Created April 4, 2013 07:13
dir2dmg.sh—a bash script to create OS X .DMG images from the given folder or folders (supports wildcards).
#!/bin/bash
if [ "$#" -eq 0 ]; then
echo "Usage:"
echo " dir2dmg <path/to/directory>"
exit 1
fi
PWD=`pwd`
@aisrael
aisrael / after_cd
Last active December 16, 2015 06:49
after_cd—Place this in `~/.rvm/hooks` and it'll look for a `.env` file every time you `cd` into a directory and export all environment variable definitions (of the form `VAR=value`) therein.
#!/usr/bin/env bash
if [ -f ".env" ]; then
while read line; do
# ignore blank lines and lines starting with '#' (comments)
if [[ ! -z $line && ! $line == \#* ]]; then
export $line
fi
done < .env
fi
NSString* path = [[NSBundle mainBundle] pathForResource: @"collections" ofType: @"json"];
NSError* e;
NSString* json = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&e];
id data = [RKNSJSONSerialization objectFromData:[json dataUsingEncoding:NSUTF8StringEncoding] error:&e];
NSMutableArray* collections = [[NSMutableArray alloc] initWithCapacity:[data count]];
for (id record in data) {
Collection* collection = [Collection new];
RKMappingOperation* op = [[RKMappingOperation alloc] initWithSourceObject:data destinationObject:collections mapping:[Collection mapping]];
[op start];
@aisrael
aisrael / gist:7019350
Last active December 25, 2015 18:19
A Scala worksheet that exhibits non-associative behaviour on line 38, in the definition of `union` in class `NonEmpty`. With the given expression, `((left union right) union other)`, `largeSet.union(Empty)` takes an inordinate amount of time to complete with sets with 100 elements or more. When that expression is changed to `(left union (right u…
package week3
object intsets {
util.Properties.versionString //> res0: String = version 2.10.2
// 1..100, shuffled
val largeSet = util.Random.shuffle((1 to 100).toList).foldLeft(Empty: IntSet)(_.incl(_))
Empty.union(Empty)
Empty.union(largeSet)
largeSet.union(Empty)
@aisrael
aisrael / gist:7020867
Last active December 25, 2015 18:29
An updated version of https://gist.github.com/AlistairIsrael/7019350, that shows how even with larger sets/trees with random elements, the expression ((left ∪ right) ∪ other) can take forever but (left ∪ (right ∪ other)) will finish instantly.
package week3
import util.Random;
object intsets {
util.Properties.versionString
// Return the (approx) number of seconds elapsed after calling the given block
def time[R](block: => R): Double = {
val t0 = System.nanoTime()
@aisrael
aisrael / git-export
Last active August 29, 2015 14:00
Provides a "git export" command similar to "svn export" that exports the current index (including all submodules) to the target path.
#!/bin/bash
#
# Place this in your executable path (e.g. "~/bin/git-export")
# and you should now be able to go: git export [path]
PWD=$(pwd)
# Exit on error (e.g., "Not a git repository")
set -e
TOP_LEVEL=$(git rev-parse --show-toplevel)
@aisrael
aisrael / docker_bashrc
Last active August 29, 2015 14:01
A bunch of useful bash functions and aliases for Docker.
# Use 'docker.io' if using the official packages on Ubuntu 14.04+
alias dip="docker inspect --format '{{ .NetworkSettings.IPAddress }}'"
drm() { docker rm ; }
dri() { docker rmi ; }
alias dkd="docker run -d -P"
alias dki="docker run -t -i -P"
alias docker_rmi_all='docker rmi $(docker images|tail -n +2|awk '\''{print $1":"$2}'\'')'
#####