Skip to content

Instantly share code, notes, and snippets.

View cbrumelle's full-sized avatar

Colin Brumelle cbrumelle

View GitHub Profile
"This actually did happen to a real person, and the real person is me. I had gone to catch a train, This was April 1976, in Cambridge, U.K.
I was about twenty minutes early.
I'd got the time of the train wrong. I suppose it is at least equally possible,"
he added after a moment's reflection,
"that British Rail had got the time of the train wrong. Hadn't occurred to me before."
"Get on with it." Fenchurch laughed.
"So I bought a newspaper, to do the crossword, and went to the buffet to get a cup of coffee."
"You do the crossword?"
"Yes."
"Which one?"
# Currently in Rails 2 and 3...
# https://github.com/rails/rails/blob/3-2-stable/actionpack/lib/action_dispatch/http/headers.rb
#
# headers = ActionDispatch::Http::Headers.new
# headers['HTTP_FOO_BAR'] = 'a1b2c3'
# puts headers['foo-bar'] #=> 'a1b2c3'
# puts headers.has_key? 'foo-bar' #=> false
# puts headers.has_key? 'HTTP_FOO_BAR' #=> true
#
# I found this pseudo hash syntax, where the array [] lookup returned a value,
require 'irb/completion'
ARGV.concat [ "--readline",
"--prompt-mode",
"simple" ]
require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb_history"
# load wirble
@cbrumelle
cbrumelle / humanize_large_number.rb
Created November 5, 2010 18:37
Format large numbers nicely for millions, billions and trillions
# Format large numbers nicely for Millions, Billions and Trillions
# Example:
# humanize_large_number(123456789) => 123.46M
# humanize_large_number(123) => 123
def humanize_large_number(i)
i = Integer(i)
case i
when 1000000..999999999
"%.2fM" % (i/1000000.0)
@cbrumelle
cbrumelle / Distance.sql
Created July 22, 2010 16:57
Calculate distance in MySQL from Points
DELIMITER $$
DROP FUNCTION IF EXISTS `test`.`distance` $$
CREATE FUNCTION `distance`(a POINT, b POINT) RETURNS double
DETERMINISTIC
COMMENT 'distance function'
BEGIN
RETURN round(glength(linestringfromwkb(linestring(asbinary(a), asbinary(b)))));
END $$
@cbrumelle
cbrumelle / sequel_to_json.rb
Created July 13, 2010 01:00
Add 'to_json' method to Sequel datasets
# Monkey patch/hack for adding in a to_json for Sequel datasets
# See http://sequel.rubyforge.org
class Sequel::Dataset
def to_json
naked.all.to_json #Note: 'naked' converts dataset to plain hash...
end
end
@cbrumelle
cbrumelle / ycombinator-example.js
Created June 28, 2010 19:49
ycombinator in Javascript
// From: http://matt.might.net/articles/implementation-of-recursive-fixed-point-y-combinator-in-javascript-for-memoization/
//
// A "functional" is just a function that takes
// another function as input.
// The Y combinator finds the fixed point
// of the "functional" passed in as an argument.
// Thus, the Y combinator satisfies the property:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script src="prototype.js"></script>
<script language="javascript" type="text/javascript" src="jsonp.js"></script>
<title>JSONP test page</title>
</head>
@cbrumelle
cbrumelle / kd-tree.rb
Created March 10, 2010 21:23
KD-TREE's in ruby
require 'pp'
class KDTree
attr_reader :root
attr_reader :points
def initialize(points, dim)
@dim = dim
@root = KDNode.new(dim).parse(points)
end
<!-- include the data file. This dumps everything into a JS variable called 'data' -->
<script src="/data/genres.js" type="text/javascript"></script>
<!-- print out some trivial data to show that it's loaded -->
<script>
$('container').update("Total count from trackdata is: " +
data.trackdata.total +
" <br />and total count from listendata is: " +
data.listendata.total);
</script>