Geohash toy: code released

A couple of weeks ago I wrote about a small toy app I'd written to explore geohashes. Now I've cleaned the code up a little, upgraded it to rails 3.1 and released it here on github. Enjoy.

Tip: Relative paths with File.expand_path

You probably know about the __FILE__ magic constant. It holds the filename of the currently executing ruby source file, relative to the execution directory. So with the following saved as /code/examples/path_example.rb:

puts __FILE__

Running this file from the /code folder will output examples/path_example.rb

This is often used to load files on paths relative to the current file. The way I've used it before is like this:

config_path = File.expand_path(File.join(File.dirname(__FILE__), "config.yml"))

This works, but it's a bit clunky.

What I didn't realise until reading the rails source code the other day, is that File.expand_path can take a second argument - a starting directory. Also, this argument doesn't actually have to be a path to a directory, it also accepts a path to a file. With this knowledge we can shorten the above to the following:

config_path = File.expand_path("../config.yml", __FILE__)

Much simpler.

Tip: Zoom keyboard shortcut for OS X

In the Terminal run:

defaults write NSGlobalDomain NSUserKeyEquivalents '{"Zoom" = "@^Z"; "Zoom Window" = "@^Z"; }'

Quit and relaunch your applications, and ⌃⌘Z should zoom and unzoom.

Stolen from macoshints.com, posted here for my own benefit.

Tip: cdpath - Am I the last to know?

This one is just so simple, I can't believe I didn't know about it earlier.

First, setup the cdpath or CDPATH variable:

cdpath=(~ ~/Projects/apps ~/Projects/tools ~/Projects/plugins ~/Projects/sites)

Now, changing directory in the shell becomes a whole world easier:

tomw@fellini:~$ cd super-secret-app
~/Projects/apps/super-secret-app
tomw@fellini:~/Projects/apps/super-secret-app$ cd Documents
~/Documents
tomw@fellini:~/Documents$ cd tomafro.net
~/Projects/sites/tomafro.net
tomw@fellini:~/Projects/sites/tomafro.net $

I've already added this to my dotfiles.

Tip: Open new tab in OS X Terminal

Another simple shell function, this time just for OS X.

Usage is simple: tab <command> opens a new tab in Terminal, and runs the given command in the current working directory. For example tab script/server would open a new tab and run script/server.

tab () {
  osascript 2>/dev/null <<EOF
    tell application "System Events"
      tell process "Terminal" to keystroke "t" using command down
    end
    tell application "Terminal"
      activate
      do script with command "cd $PWD; $*" in window 1
    end tell
  EOF
}

Tip: Create and move to directory

Before my next post on database indexes, here's a useful little function from the Advanced Command Line peepcode screencast (which I highly recommend).:

# Create and move to a directory in a single command
# Usage: take ~/Projects/tools/awesometer

take() {
  mkdir -p $1
  cd $1
}

Tip: Move to directory and open in TextMate

Since adding this to my zsh configuration, I'm finding I use it all the time:

# Change directory and open TextMate in a single command
# Usage: tm ~/Projects/sites/tomafro.net

tm() {
  cd $1
  mate $1
}