Skip to content

Instantly share code, notes, and snippets.

View zerowidth's full-sized avatar

Nathan Witmer zerowidth

View GitHub Profile
@zerowidth
zerowidth / super_semantics.rb
Created March 4, 2010 16:26
ruby 1.8.7 p249 bug
class A
def foo
"foo!"
end
end
class B < A
def foo
lambda { super }
end
# as of 2010-04-01
export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/mysql/bin:$PATH:/Users/nathan/scripts
export PATH=/Users/nathan/bin:/Users/nathan/bin/gitflow:/opt/local/lib/postgresql84/bin:$PATH
export LD_LIBRARY_PATH=/opt/local/lib
# export EDITOR="/Users/nathan/bin/mate_wait"
# export EDITOR="/Users/nathan/bin/mvim -f" # -f means don't fork, wait for changes
export EDITOR="/opt/local/bin/vim -f"
# export EDITOR="/opt/local/bin/mvim -f"
@zerowidth
zerowidth / ar_literals.rb
Created June 22, 2010 23:19
DEFAULT literals in AR for postgres defaults (via @copiousfreetime)
# Force ActiveRecord to insert the 'DEFAULT' literal for columns which
# are non-null but do not have a default that AR understands.
#
# MySQL is lenient in that if you insert a NULL in something with a default,
# the database will just insert the default anyway. Postgres is not so kind:
# it'll do exactly what you told it, causing an error.
# Usually AR can figure out the default for a column, but if it's a default
# value that the connection adapter can't figure out, it'll insert a NULL.
# So: allow explicit 'DEFAULT' literals (which is SQL standard) to be used instead
# where appropriate.
@zerowidth
zerowidth / git_funky.sh
Created July 16, 2010 17:57
git demo of what happens when you try to push to a working copy
#!/bin/sh
# try out cloning and pushing using a working copy in git
#
function log() {
echo
echo "***** $1"
echo
}
@zerowidth
zerowidth / pivotal-post-receive.rb
Created July 25, 2010 22:19
git post-receive hook for pivotal tracker
#!/usr/bin/env ruby
# git post-receive hook that posts the log messages to pivotal tracker
# for automatic updates of stories.
# See http://www.pivotaltracker.com/help/api?version=v3#scm_post_commit
# for more information
require "cgi"
begin
@zerowidth
zerowidth / client.rb
Created July 27, 2010 01:12
module methods and super
# A rough example of how I'm putting module method overrides to use.
# This allows me to add functionality to methods without having to
# do method aliasing, and it's easy to enable/disable things too.
module Search
def search(query, opts={})
request("search", :query => query).results
end
end
@zerowidth
zerowidth / node_scope_test.js
Created August 3, 2010 15:20
js scoping demo (node.js)
var sys = require('sys');
var foo = {
name: 'foo',
bar: function() {
sys.puts('in bar, this is ' + this.name);
setTimeout(this.bar, 1000);
},
baz: function() {
var me = this;
# this is why AR3 and arel is awesome.
# bus route data, loaded from google transit data
# the usage
class StopsController < ApplicationController
def nearest
@stops = Stop.nearest_to(lon, lat).serving_route("SKIP").limit(5).all
end
end
@zerowidth
zerowidth / flatten.rb
Created November 5, 2010 16:51
generate flat map chunks for easy testing (minecraft alpha server)
#!/usr/bin/env ruby
require "fileutils"
require "pathname"
require "zlib"
world = Pathname.new File.expand_path("./world")
0.upto(3) do |x|
0.upto(3) do |z|
@zerowidth
zerowidth / mongomapper_json.rb
Created November 8, 2010 18:28
fix mongo mapper's as_json so it handles :include with sub-options (e.g. :include => {:foos => {:only => "name"}} )
module App
class Model
def as_json(opts={})
hash = super
if opts && inclusions = opts[:include]
case inclusions
when Array
inclusions.each do |key|
hash[key.to_s] = send(key.to_sym).as_json
end