Thursday, November 20, 2008

Method of the Day: index_by

Just stumbled on to this one. I can't tell you how many times I've wanted this functionality. You have an array, but you want a hash keyed by an attribute of the items in the array. Typically, you'd do something like this:
users_by_name = {}
users.each { |u| users_by_name[u.name] = u }
Which is of course silliness now that we know about index_by.
users_by_name = users.index_by(&:name)
Slick.

Thursday, November 6, 2008

Pushing to multiple user accounts on GitHub

Here at Avvo, we've started to use GitHub for publishing some of our internal libraries that might be useful to the programming community. Although we have a company account, it's nice to have a personal account, too. Unfortunately, github doesn't allow multiple accounts to share the same SSH key. This makes sense, because all communication with github works using the same ssh user account. From my perspective, it can get kind of annoying. The key to being able to check code into multiple github accounts is setting up multiple ssh keys and using ssh aliases to use the correct key for the correct repo. First, I created a company-specific key:
justin-mac:~ jweiss$ ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/jweiss/.ssh/id_rsa): /Users/jweiss/.ssh/id_rsa.avvo
Enter passphrase (empty for no passphrase): 
Enter same passphrase again:
Then I set up an SSH alias in ~/.ssh/config:
host github-avvo
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa.avvo
Next, I copied the id_rsa.avvo.pub key into the avvo github acccount. Finally, when creating the clone of whichever repo I was using, I didn't use the clone path that github gave me, I translated it to use the alias I created. For example, a clone URL like
git@github.com:avvo/fuzzy-find-in-project.git
becomes
github-avvo:avvo/fuzzy-find-in-project.git
Now, when pushing to the central repository, it will pick up the id_rsa.avvo key that I tied to the avvo github account, and send the code to the right place.