Skip to content

Instantly share code, notes, and snippets.

Andy Newport newportandy

Block or report user

Report or block newportandy

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View gist:237354
############
#Class Hash#
############
#Dijkstra's algorythm requires a priority queue, the easiest way to make one is to add a method
#to hash that pops the key/value pair with the lowest value.
class Hash
#Return the key/value pair with the lowest value as an array, deletes the pair.
def pop_lowest_value!
smallest_pair = self.min{ |a,b| a[1] <=> b[1]}
self.delete(smallest_pair[0]) if smallest_pair
@newportandy
newportandy / gist:241483
Created Nov 23, 2009
Investigating Recursive vs Iterative performance in Map traversal
View gist:241483
NODES = 0..1
LENGTH = 2
INFINITY = 1.0/0
class PriorityQueue < Hash
def pop!
smallest_pair = self.min{ |a,b| a[1] <=> b[1]}
self.delete(smallest_pair[0]) if smallest_pair
smallest_pair
end
View scaffold_from_existing_table.rb
#!/usr/bin/env /home/andy/dev/ruby/dog_application/script/runner
#In this case the script is written to be executed in the "dog_application" rails root folder.
#The table to scaffold, Really should take a command line variable...
input = "dog"
eval "class #{input.humanize} < ActiveRecord::Base
end"
columns = {}
View gist:300904
#!/bin/bash
# You can run this on your server by doing this:
# bash -c "`wget -O - frozenplague.net/boris`"
# If you don't have wget, use curl.
echo "Need your password to update time & install things:"
sudo ntpdate pool.ntp.org
sudo apt-get -y update
sudo apt-get -y install build-essential mysql-server libmysqlclient15-dev apache2 libssl-dev apache2-prefork-dev libapr1-dev libaprutil1-dev zlib1g zlib1g-dev
View README.markdown

SVN Cleaner

Git creates one .git folder in the root of a project. SVN creates a .svn/_svn folder in every single folder of it's project. This script cleans out the ( _svn | .svn ) folders left behind by subversion, it's useful when you don't already have an automated release process in place to do this and you quickly need to clean pu a checked out repo for a client/release.

Use this at your own risk.

@newportandy
newportandy / gutenberg.rb
Created Jul 22, 2010
Pull down a few public domain books
View gutenberg.rb
require 'net/http'
require 'uri'
(10000..10010).each do |i|
path = i.to_s.chars.to_a[0...-1].inject(""){|memo, value|"#{memo}/#{value}"}
path = "/dirs" + path + "/#{i}/#{i}.txt"
puts "Get: http://www.gutenberg.org" + path
Net::HTTP::Proxy("yourproxyserver", 8080).start("www.gutenberg.org", 80) do |http|
http.request_get(path) do |response|
File.open("#{i}.txt", 'w') do |file|
View validations.js
document.observe("dom:loaded", function() {
$$(".things_you_want_to_validate").each(function(item) {
if(item.value) {
var x = parseFloat(item.value);
var handler = function(event) {
var element = Event.element(event);
var old_val = x;
var new_val = parseFloat(element.value);
if (isNaN(new_val) || new_val > (old_val * 1.2) || new_val < (old_val * .8)) {
element.addClassName('warning');
You can’t perform that action at this time.