Skip to content

Instantly share code, notes, and snippets.

@dmoulton
dmoulton / postnet_bin.rb
Created October 26, 2010 20:03
Returns a binary representation of a POSTNET encoded zip code, suitable for converting to an image
#returns binary representation of a POSTNET encoded zip code
#could be used to filter a zip code and then generate an image
#1 = full bar, 0 = half bar
class PostnetBin
def self.convert(zip)
codes = ["11000","00011","00101","00110","01001","01010","01100","10001","10010","10100"]
r = "1" #bookend
total = 0
zip.gsub(/[^\d]/,'').each_char do |c|
r += codes[c.to_i]
@dmoulton
dmoulton / gist:711302
Created November 23, 2010 05:16
state abbreviation hash
state = {
'AL' => 'Alabama',
'AK' => 'Alaska',
'AS' => 'America Samoa',
'AZ' => 'Arizona',
'AR' => 'Arkansas',
'CA' => 'California',
'CO' => 'Colorado',
'CT' => 'Connecticut',
'DE' => 'Delaware',
@dmoulton
dmoulton / passenger-control
Created December 22, 2010 01:18
control script for stand alone passenger
#!/bin/sh
#meant to reside in script/ directory of a rails install
PORT=8200
ROR_ENV=production
LOC=`dirname $0`
case "$1" in
start)
passenger start -p $PORT -e $ROR_ENV -d
;;
@dmoulton
dmoulton / sitegen.rb
Created January 28, 2011 16:20
Rails 3 generator template for generating a site that has a small cms, admin users, admin groups, and is multisite capable.
create_file ".rvmrc", "rvm #{`rvm-prompt`}"
generate("scaffold","admin_user username:string password:string name:string email:string")
generate("scaffold","admin_group name:string description:string")
generate("migration","create_admin_group_admin_user")
insert_into_file("db/migrations/\*create_admin_group_admin_user.rb", " add_column :admin_group_admin_user :admin_group_id, :integer\n add_column :admin_group_admin_user :admin_user_id, :integer\n", :after=>"def self.up")
insert_into_file("db/migrations/\*create_admin_group_admin_user.rb", " remove_column :admin_group_admin_user :admin_group_id\n remove_column :admin_group_admin_user :admin_user_id\n", :after=>"def self.down")
##################################################
# Fancy PWD display function
##################################################
# The home directory (HOME) is replaced with a ~
# The last pwdmaxlen characters of the PWD are displayed
# Leading partial directory names are striped off
# /home/me/stuff -> ~/stuff if USER=me
# /usr/share/big_dir_name -> ../share/big_dir_name if pwdmaxlen=20
##################################################
bash_prompt_command() {
@dmoulton
dmoulton / hist
Created March 7, 2011 20:37
Shortcut for seeing your recent bash history. Put it somewhere in your path. hist 20 gives last 20 commands. hist gives last 10
history | tail -$1
@dmoulton
dmoulton / combinerepos.txt
Created August 17, 2011 17:17
How to combine git repositories into one
Steps to combine multiple git repos into one
0. Make sure all repos you are moving are checked in and backed up
1. cd projects/project1
2. mkdir proj
3. git mv * proj/ #will error when copying project1 directory to itself
4. git commit -m "about to migrate to new repo"
repeat 1 - 4 for other repos you want to combine
@dmoulton
dmoulton / ghist
Created August 17, 2011 17:29
grep your shell history
history | grep -i "$1"
@dmoulton
dmoulton / match_do_regex
Created November 15, 2011 21:14
Regular expression challenge
I need a regular expression that matches the word 'do' on this line:
things.each do |x|
but not on this line:
x = "do your worst"
Specifically, I'd like to match on the word 'do' only when it is NOT followed by a quote character later on the same line.
@dmoulton
dmoulton / Main.sublime-menu
Created January 27, 2012 04:00
Non interactive ruby REPL for Sublime Text 2
[
{
"id": "tools",
"children":
[{
"caption": "SublimeREPL",
"mnemonic": "r",
"id": "SublimeREPL",
"children":
[