Skip to content

Instantly share code, notes, and snippets.

@mxcl
mxcl / fixnum+base62.rb
Created February 6, 2010 16:17
Readable Base 62 encoder
# Use this for your URL shortening needs
# Base 62 is as good as you can easily get for URL shortening
# And it's effective: 1_000_000 => '4c92'
# Challenge: fork and speed up the implementation!
class Fixnum
TOKENS = ('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a
# NOTE returns nil for 0
def to_62_s
@mxcl
mxcl / tell
Created February 4, 2010 13:40
Convenience script for running certain Applescripts
#!/usr/bin/ruby
#
# Example usage:
# tell iTunes to pause
# tell iTunes to play
# tell Linkinus to quit
#
# http://twitter.com/mxcl
# we add it back later
@mxcl
mxcl / fswatcher.c
Created November 27, 2009 10:38
C code to watch a directory for changes
// gcc -o fswatcher -framework Carbon fswatcher.c
#include <CoreServices/CoreServices.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/dir.h>
static time_t timestamp = 0;
static void callback(ConstFSEventStreamRef stream,
# This is more "Ruby", and I agree that I don't necessarily agree with
# all the syntax choices. The else condition isn't required as the
# function returns nil if no other code paths executed. You could argue
# that, that is a regression waiting to happen though.
def just_one_kind_of_response? item, controller
case controller
when 'actions'
item.comments.blank? or item.suggestions.blank? or detail.comments.blank?
when 'thoughts'
@mxcl
mxcl / rsync+watcher.rb
Created July 18, 2009 09:32
rsync whenever the directory the script is in changes using the OS X FSEvents API
#!/usr/bin/ruby
require 'osx/foundation'
OSX.require_framework '/System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework'
include OSX
$d=File.basename Dir.pwd
Dir.chdir '..'
fsevents_cb = proc do |stream, ctx, numEvents, paths, marks, eventIDs|
system "rsync --delete -rt --rsh=ssh '#{$d}' 'methylblue.com:public_html/'"
@mxcl
mxcl / flac2mp3.md
Created June 5, 2009 11:28
Simplest functional FLAC to MP3 converter script you can make
@mxcl
mxcl / .profile
Created April 22, 2009 16:13
My Bash .profile file
#!/bin/bash
alias ..="cd .."
alias ls="ls -p"
alias la="ls -lA"
alias l="ls -l"
alias ll="ls -l"
alias psf="ps -cU `whoami`"
alias grep="grep --color=auto"
alias cack="ack --cpp"