Skip to content

Instantly share code, notes, and snippets.

View davidrichards's full-sized avatar

David Richards davidrichards

View GitHub Profile
var w = 1280,
h = 800,
r = 720,
x = d3.scale.linear().range([0, r]),
y = d3.scale.linear().range([0, r]),
node,
root;
var pack = d3.layout.pack()
.size([r, r])
greeting = "Hello"
def dumb_greeter
puts greeting
end
def invasive_greeter(b)
puts b.eval("greeting")
end
@davidrichards
davidrichards / Rakefile
Created August 2, 2012 21:14
A basic sinatra example
require_relative './database'
require 'sinatra/activerecord/rake'
@davidrichards
davidrichards / amortization_table.rb
Created July 24, 2012 07:47
Just so I don't forget and have to re-figure this another time in my life. This isn't optimized and does nothing to handle rounding errors.
class AmortizationTable
Transaction = Struct.new(:payment, :interest, :principle, :balance)
attr_reader :amount, :rate, :n
def initialize(opts={})
@amount = opts.fetch(:amount, 25_000)
@rate = opts.fetch(:rate, (0.06 / 36))
@n = opts.fetch(:n, 36)
end
@davidrichards
davidrichards / models.rb
Created July 17, 2012 19:54
Just some associations to consider
rails g model Title name
rails g model IssuesTitle issue_id:integer title_id:integer
rails g model Issue name publisher_id:integer illustration
rails g model Publisher name
rails g model Role name
rails g model Person name
rails g model PersonRole name issue_id:integer role_id:integer person_id:integer
@davidrichards
davidrichards / spike
Created July 4, 2012 17:16
spike: an easy way to start throw-away code (and still test drive)
#!/usr/bin/env ruby
require 'fileutils'
class SpikeBuilder
def self.build!(name, gemset)
obj = new(name, gemset)
obj.build!
end
@davidrichards
davidrichards / chart.js
Created June 26, 2012 01:23
A basic closure example. Also here: http://bost.ocks.org/mike/chart/
function chart() {
var width = 720, // default width
height = 80; // default height
function my() {
// generate chart here, using `width` and `height`
}
my.width = function(value) {
if (!arguments.length) return width;
@davidrichards
davidrichards / a.rb
Created June 19, 2012 02:51
Basic Includes and Extends
require 'forwardable'
class A
include Enumerable
extend Forwardable
def_delegators :@source, :[], :<<
def initialize
@source = []
end
def each(&block)
@davidrichards
davidrichards / .tmux.conf
Created June 15, 2012 01:27
The Tmux config that works on my server (some things commented out currently)
set -g prefix C-a
unbind C-b
bind C-a send-prefix
set -sg escape-time 1
set -g base-index 1
# setw -g pane-base-index 1
bind r source-file ~/.tmux.conf \; display "Reloaded!"
@davidrichards
davidrichards / Mediator.coffee
Created April 24, 2012 04:49
Using Backbone.js with a Mediator
# This is borrowed from http://bit.ly/K4h5xl
window.Support = (window.Support || {})
window.Support.Mediator = (params) ->
self = {}
self.register = (module) ->
module.subscribe = self.subscribe
module.publish = self.publish