Skip to content

Instantly share code, notes, and snippets.

View seven1m's full-sized avatar

Tim Morgan seven1m

View GitHub Profile
@seven1m
seven1m / american_date_parse.rb
Created September 17, 2009 04:50
swap american date order so Ruby 1.9 Date._parse will understand it
# complete hack to swap date MM/DD/YYYY around to YYYY/MM/DD
# so Ruby 1.9 will parse American dates the way we like 'em
# (works in Rails due to use of alias_method_chain)
class Date
class << self
def _parse_with_american_date(str, comp=true)
_parse_without_american_date(str.sub(/(\d{1,2})\/(\d{1,2})\/(\d{4})/, '\3/\1/\2'), comp)
end
alias_method_chain :_parse, :american_date
{
/* Remap Home / End to be correct on Mac */
"\UF729" = "moveToBeginningOfLine:"; /* Home */
"\UF72B" = "moveToEndOfLine:"; /* End */
"$\UF729" = "moveToBeginningOfLineAndModifySelection:"; /* Shift + Home */
"$\UF72B" = "moveToEndOfLineAndModifySelection:"; /* Shift + End */
}
@seven1m
seven1m / number_to_human_size.js
Created November 12, 2009 16:45
matching ruby and javascript methods for formatting bytes in human readable format
function number_to_human_size(size) {
if(size < 1024)
return size + ' bytes';
else if(size < 1024.0 * 1024.0)
return (size / 1024.0).toFixed(1) + ' KiB'
else if(size < 1024.0 * 1024.0 * 1024.0)
return (size / 1024.0 / 1024.0).toFixed(1) + ' MiB'
else
return (size / 1024.0 / 1024.0 / 1024.0).toFixed(1) + ' GiB';
}
@seven1m
seven1m / unescape.rb
Created December 3, 2009 21:21
dirty little script I used to fix escaped character sequences in a yaml file
lines = File.read(ARGV.first).split("\n")
File.open(ARGV.last, 'w') do |out|
lines.each do |line|
if line =~ /:/
key, value = line.split(/:\s*/, 2)
value = eval(value) if value =~ /^"/
if value =~ /\{|"|:/
value.gsub!(/"/, "\\\"")
value = "\"#{value}\""
@seven1m
seven1m / nested_group_by.rb
Created January 12, 2010 05:44
A couple different ways of doing nested group_by. There are surely more...
require 'test/unit'
Person = Struct.new(:name, :age, :gender)
class GroupByTest < Test::Unit::TestCase
def setup
@people = [
@jennie = Person.new('Jennie', 29, 'f'),
@justin = Person.new('Justin', 28, 'm'),
@seven1m
seven1m / gist:332025
Created March 14, 2010 15:27
remove color profile junk from png
pngcrush -rem gAMA -rem cHRM -rem iCCP -rem sRGB infile.png outfile.png
@seven1m
seven1m / gist:335453
Created March 17, 2010 16:59
Find blocking processes on SQL Server 2005
# I don't pretend to understand this query,
# but it seems to be a good list of processes
# that are "blocking" other processes and hosing the server.
select * from sys.dm_os_waiting_tasks where session_id != @@spid and session_id > 50 and blocking_session_id < session_id order by wait_duration_ms desc;
@seven1m
seven1m / dedup_emails_for_scott.rb
Created March 26, 2010 04:36
Mr. Scott needed some Wufoo importing love...
#!/usr/bin/env ruby
# dedup_emails_for_scott.rb
# usage:
# ruby dedup_emails_for_scott.rb file_to_upload.csv existing_entries.csv file_to_upload_deduped.csv
EMAIL_COL = 'Email' # change this if your column name is not 'Email'
if ARGV.length == 3
upload_filename, existing_filename, output_filename = ARGV
else
; my first clojure program -- pings a subnet looking for existing hosts
; usage (you'll need a clj repl script):
; clj pingdom.clj 192.168.2
; clj pingdom.clj --used 10.0.0
;
(ns pingdom
(:use [clojure.contrib.shell-out :only (sh)])
(:use [clojure.contrib.str-utils :only (re-split str-join)])
(:use [clojure.contrib.string :only (replace-first-re)]))
require 'pp'
class Card
attr_reader :face_up
def initialize(face_up)
@face_up = face_up
end
def flip