Skip to content

Instantly share code, notes, and snippets.

View carlzulauf's full-sized avatar

Carl Zulauf carlzulauf

  • Unabridged Software
  • Denver, CO
View GitHub Profile
@carlzulauf
carlzulauf / haversine.sql
Created February 2, 2012 16:47
PostgreSQL function for haversine distance calculation, in miles
-- Haversine Formula based geodistance in miles (constant is diameter of Earth in miles)
-- Based on a similar PostgreSQL function found here: https://gist.github.com/831833
-- Updated to use distance formulas found here: http://www.codecodex.com/wiki/Calculate_distance_between_two_points_on_a_globe
CREATE OR REPLACE FUNCTION public.geodistance(alat double precision, alng double precision, blat double precision, blng double precision)
RETURNS double precision AS
$BODY$
SELECT asin(
sqrt(
sin(radians($3-$1)/2)^2 +
sin(radians($4-$2)/2)^2 *
@carlzulauf
carlzulauf / geodist.rb
Created February 2, 2012 16:57
Ruby haversine distance method, in miles
def geodist(a, b)
rads = Math::PI/180
# 7926.3352 is diameter of earth in miles
7926.3352 * Math.asin(
Math.sqrt(
Math.sin( (b[1] - a[1])*rads / 2 )**2 *
Math.cos( a[0] * rads ) *
Math.cos( b[0] * rads ) +
Math.sin( (b[0] - a[0])*rads / 2 )**2
)
@carlzulauf
carlzulauf / paperclip.rb
Created February 9, 2012 02:26
paperclip attachment remote file download
require 'open-uri'
def get_file(url)
filename = url.split("/").last.gsub(/_[0-9a-f\-]{36}\./,".")
begin
file = open( url )
file.instance_variable_set(:@original_filename, filename)
def file.original_filename; @original_filename; end
file
rescue OpenURI::HTTPError
[user]
name = Your Full Name
email = [email protected]
[alias]
st = status
d = diff
co = checkout
ci = commit -v
cia = commit -v -a
b = branch
class STOP < Exception
def initialize(msg)
super
puts "STOP! #{msg}"
end
def time; Time.now; end
end
def Time.cant_touch_this
@carlzulauf
carlzulauf / perm.sql
Created June 24, 2012 23:57
Create mysql power user
CREATE USER username IDENTIFIED BY 'password';
GRANT ALL ON *.* TO username@localhost IDENTIFIED BY 'password';
This file has been truncated, but you can view the full file.
function getNovel(){
return
{"title":"War and Peace","content":"The Project Gutenberg EBook of War and Peace, by Leo Tolstoy\r\n\r\nThis eBook is for the use of anyone anywhere at no cost and with\r\nalmost no restrictions whatsoever. You may copy it, give it away or\r\nre-use it under the terms of the Project Gutenberg License included\r\nwith this eBook or online at www.gutenberg.org\r\n\r\n\r\nTitle: War and Peace\r\n\r\nAuthor: Leo Tolstoy\r\n\r\nPosting Date: January 10, 2009 [EBook #2600]\r\nRelease Date: April, 2001\r\n[Last updated: August 22, 2012]\r\n\r\nLanguage: English\r\n\r\n\r\n*** START OF THIS PROJECT GUTENBERG EBOOK WAR AND PEACE ***\r\n\r\n\r\n\r\n\r\nAn Anonymous Volunteer\r\n\r\n\r\n\r\n\r\n\r\nWAR AND PEACE\r\n\r\nBy Leo Tolstoy/Tolstoi\r\n\r\n\r\n\r\n\r\n\r\nBOOK ONE: 1805\r\n\r\n\r\n\r\n\r\n\r\nCHAPTER I\r\n\r\n\r\n\"Well, Prince, so Genoa and Lucca are now just family estates of the\r\nBuonapartes. But I warn you, if you don't tell me that this means war,\r\nif you still try to de
This file has been truncated, but you can view the full file.
function getNovel(){
return {"title":"War and Peace","content":"The Project Gutenberg EBook of War and Peace, by Leo Tolstoy\r\n\r\nThis eBook is for the use of anyone anywhere at no cost and with\r\nalmost no restrictions whatsoever. You may copy it, give it away or\r\nre-use it under the terms of the Project Gutenberg License included\r\nwith this eBook or online at www.gutenberg.org\r\n\r\n\r\nTitle: War and Peace\r\n\r\nAuthor: Leo Tolstoy\r\n\r\nPosting Date: January 10, 2009 [EBook #2600]\r\nRelease Date: April, 2001\r\n[Last updated: August 22, 2012]\r\n\r\nLanguage: English\r\n\r\n\r\n*** START OF THIS PROJECT GUTENBERG EBOOK WAR AND PEACE ***\r\n\r\n\r\n\r\n\r\nAn Anonymous Volunteer\r\n\r\n\r\n\r\n\r\n\r\nWAR AND PEACE\r\n\r\nBy Leo Tolstoy/Tolstoi\r\n\r\n\r\n\r\n\r\n\r\nBOOK ONE: 1805\r\n\r\n\r\n\r\n\r\n\r\nCHAPTER I\r\n\r\n\r\n\"Well, Prince, so Genoa and Lucca are now just family estates of the\r\nBuonapartes. But I warn you, if you don't tell me that this means war,\r\nif you still try to de
@carlzulauf
carlzulauf / irbrc.rb
Created January 15, 2013 17:44
Example irbrc to show using rails or gem specific features when they are available.
require 'pp'
require 'bigdecimal'
File.join(ENV["PWD"],'lib').tap do |lib|
if File.directory?(lib)
$LOAD_PATH.unshift(lib) unless defined?(Rails)
end
end
class BigDecimal
@carlzulauf
carlzulauf / hash_struct.rb
Created January 20, 2013 14:03
Hash initialized Struct
class HashStruct
def self.new(*args)
Struct.new(*args) do
def initialize(hash)
super( *self.class.members.map{|k| hash[k] } )
end
end
end
end