Skip to content

Instantly share code, notes, and snippets.

View dbrady's full-sized avatar

David Brady dbrady

View GitHub Profile
@dbrady
dbrady / utah_fire_omg.rb
Created June 28, 2012 21:43
Ruby script to generate an animated map of Utah that is very, VERY on fire
#!/usr/bin/env ruby
#
# Generate an astonishingly accurate, animated map of Utah with
# thousands of wildfire pins scattered around it by throwing dozens of
# random lat/lng pairs at the google static maps API.
# NOTE: This script requires ImageMagick. Good luck with that.
# returns a latitude and longitude that lies within the state of Utah
#
@dbrady
dbrady / rand.sh
Created April 6, 2012 05:48
Rand using bc
#!/bin/sh
# Linear Congruential Generator using bc
#
# Note the shenanigans necessary to seed the generator with the epoch
# time: $(date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s") does nothing
# more than return the current date in epoch seconds, which we use to
# seed the generator. This generator is due to Knuth, and cited in
# Numerical Recipes in C, chapter 7. It's fast on old 32-bit machines
# but not a very good RNG:
@dbrady
dbrady / dangeromatic.rb
Created March 16, 2012 16:17
Graph Splitter Complexity-o-Mat (Inspired by Jack Danger)
# Inspired by Jack "Best Name Ever" Danger Carty's talk at MWRC 2012
def cplx(n)
(n**2-n)/2
end
def dangeromat(n)
puts "Interconnection Complexity: #{cplx(n)}"
b=n/2; a=n-b
puts "If you split your project evenly an app and a service api, you get"
puts "THE DANGER-O-MATIC EFFECT!!!"
@dbrady
dbrady / descendants.rb
Created March 15, 2012 21:14
Return the list of descendants of a class
class Class
def extend?(klass)
superclass && (superclass == klass || superclass.extend?(klass))
end
def descendants
Module.constants.select do |constant_name|
constant = eval(constant_name.to_s)
if constant && constant.is_a?(Class) && constant.extend?(self)
constant
@dbrady
dbrady / day_labels.rb
Created March 12, 2012 23:52
pattern for maps that take n-dimensional inputs and yield m-dimensional inputs
# Draw day labels, e.g. "Mon 1/30", "Tue 1/31", "Wed 2/1" etc.
day_labels = (0...DAYS_PER_WEEK).map {|d| (start_date + d).strftime("%a %-m/%-d")}
# HEY GISTERS LOOK HERE: Okay, I have a loop that iterates 0 to
# 6, and I need this for the day_label in question, but I *also*
# need to know the x-coordinate at which to draw the label. What
# I would like in this SPECIFIC case, is a loop construct that
# somehow yields the day_label and x-coordinate, perhaps as a
# pair, which I guess I could do with a map. But in general I'm
# looking for a loop construct that accepts n-dimensional inputs
@dbrady
dbrady / gist:2023535
Created March 12, 2012 17:34
Iterating by columns with a nudge factor
We couldn’t find that file to show.
@dbrady
dbrady / vector3.rb
Created March 7, 2012 04:06
Ruby 3D Vector w/UTF
#!/usr/bin/env ruby
# encoding: utf-8
class Vector
attr_accessor :x, :y, :z
def initialize(x,y,z)
@x,@y,@z = x,y,z
end
@dbrady
dbrady / bfr.rb
Created March 6, 2012 02:28
Brainfuck to Ruby compiler
#!/usr/bin/env ruby
# bfr - brainfuck 2 ruby
# Usage:
# ./bfr "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."
# => Hello World!
INDENT = " "
START_CODE = <<CODE
@dbrady
dbrady / nested_contexts.rb
Created October 19, 2011 23:59
Is this good RSpec style?
# RSpec style question for you here.
#
# Here is the code being tested:
#
# def extractor_name
# options[:extractor_name] || name
# end
#
# My question is, I feel like I'm doing a lot of setup and
@dbrady
dbrady / lolrails.rb
Created October 17, 2011 03:54
ActiveRecord models deny their ancestry. What gives?
class Bonk < ActiveRecord::Base
end
Bonk.ancestors.include? ActiveRecord::Base
# => true
Bonk.is_a? ActiveRecord::Base
# => false
# um...WAT