Awkward and Proud

Top 10 things GitHub will do with VC Funding

GitHub is getting a round of VC funding. Here are the top 10 things they plan on using the money for.

  1. Genetically engineer an actual octocat.
  2. Pay off the anonymous Italian who's been squatting on "code.it"
  3. Upgrade Rick Olsen to "technosausage"
  4. Mojombo will finally buy back the username "tom" from the MySpace guy.
  5. Build a room just for holding their giant piles of cash.
  6. Get the lunar datacenter they've been eying for months.
  7. Develop a post-receive hook that actually kills a kitten when someone force pushes.
  8. Buy Linus.
  9. Fund the covert operation to eliminate the out-of-control self-aware Hubot.
  10. Have a drinkup.

Simulate network latency for application testing

Sometimes it's useful to test your web application for situations where internet connections are less than stellar. It turns out that Mac OS X has a builtin utility called ipfw than, among other things, can do just this.

I first saw this technique from Joe Miller's post on the subject. I packaged up the settings he mentioned into a little shell script:

You can drop that somewhere in your $PATH and chmod +x to make it executable. You can call it whatever you want, but I called mine "hinder". After that, it's simply a matter of using it:

$ hinder www.google.com

Now when you visit google.com, you should see some marked slowness. To reset, just run:

$ hinder reset

Google is now fast again.

The script works by adding 250ms delay to both directions of network traffic. It also adds a packet-loss percentage of 10%. You can play with these numbers to get even more latency simulation. Enjoy!

Add untracked files in git

I've often been annoyed that there's no way to non-interactively add untracked files in git. Well, I finally whipped an alias to speed that process up:

Just add it to your global git config file at ~/.gitconfig and enjoy.

Faster TDD feedback with tmux, tslime.vim, and turbux.vim

tl;dr Developing in a tmux session has sped up my TDD cycle considerably, especially with the help of tslime.vim and turbux.vim. I get immediate feedback in one pane, but never have to leave vim or lose context of what I'm working on to see the test result.

Taking a cue from Xavier Shay's excellent intro to tmux, I've been using tmux lately as my primary workspace. There are excellent introductions to tmux elsewhere, but I've really enjoyed the switch from MacVim/Terminal to a single tmux session for development. But rather than sing tmux's praises, I'd like to talk about how tmux and a vim plugin have changed my testing feedback loop for the better.

Other test-running solutions

Autotesting gives you immediate feedback, but runs everytime you save a file. Even though this often is desired behavior, I can't tell you how many times I've saved a feature file, only to immediately notice a typo. Especially with Rails project, this can be an expensive amount of time. I end up feeling punished for saving my work.

I've also tried more editor-embedding techniques of running tests. Both rails.vim and rake.vim provide facilities for running :Rake. When combined with a keyboard shortcut, this gets closer to the kind of control I like to have, running my tests exactly when I want them. The downside, though, is that I lose control of my editor and have to wait for the command to finish before I can type, or even navigate again. And I can't look at a failure message and my code at the same time.

A faster feedback loop

A practice that is quickly gaining popularity in the Ruby community is isolating your business logic from your persistance logic and framework. Rather than load Rails (or some other large library or framework), you sequester all business logic in its own class or module, and then test that class or module in isolation. This has a ton of benefits for the longevity of your code, but one of the side benefits is the speed increase for running individual specs or tests. This technique is being championed by Gary Bernhardt and Corey Haines, among others.

Because tmux is so scriptable, it isn't hard to send commands to other panes in a tmux session programmatically. Leveraging the power of rails.vim and tslime.vim, I've created a vim plugin that shortens the feedback loop when practicing TDD in a tmux session. It's called turbux.vim.

Using tslime.vim

My typical workflow now involves setting up a tmux session for my project, splitting vertically (<C-b> %), and using layout 4 (<C-b> <Alt-4>). In fullscreen, the result is about 30% for my shell on the left, and 70% for vim on the right.

The first time you use it, tslime.vim will prompt you to input your tmux session name, window number, and pane number. There is completion for each of these prompts, so you can happily mash <Tab>.

The plugin exposes a general-purpose function to send arbitrary text to the configured tmux pane. For example, you can use it in the following way:

:call Send_to_Tmux("rspec ".expand("%")."\n")

The above command would send rspec path/to/spec.rb to the configured pane. For me, this pattern of running the test file that is currently open is so common that I've packaged up some useful defaults in turbux.vim.

Using turbux.vim

Turbux.vim tries to intelligently choose the right spec, test or feature to run when you invoke it. If you're in a spec, invoking the plugin (by default with <leader>t) will run rspec path/to/my_spec.rb in the corresponding pane. In a test-unit file, it will run ruby -Itest path/to/test.rb. In a cucumber feature file, it will run cucumber path/to/my.feature.

Thanks to rails.vim's awesomeness, I've also provided some mappings for when the current file has a corresponding test or spec. For example, When I'm in a file that has a corresponding spec, such as a model or controller, the command will run the that spec.

Finally, if the plugin is invoked outside the context of any feature or spec-related file, it will simply run the most recent test command again.

Also, I've added a mapping for <leader>T to run a more focused spec or cucumber scenario. It works by adding the vim cursor's line number to the rspec or cucumber command.

Conclusion

This setup has been really rewarding so far. There's far less context switching, as I never have to leave my editor. There are also fewer surprises. As far as I'm concerned, the faster my feedback, the better.

Note: You will probably want to use my fork of tslime.vim, as the main repository has some outstanding bugs, and the fixes have yet to been merged in.

Bonus

See the plugin in action. If the video is hard to see, visit vimeo. There is a link to download at full size.

zsh completion for hitch

At hashrocket, we use hitch to manage our paired git authors, written by our own Rogelio Samour.

Hitch works by setting the GIT_AUTHOR_NAME and GIT_AUTHOR_EMAIL environment variables. For the email itself, it joins the hitched author's github usernames and a prefix with a "+", creating email addresses of the form "dev+jgdavey+therubymug@hashrocket.com". Using it is as simple as hitch <github_username> <github_username>.

Recently, I was annoyed that I had to always remember the github username of the person I was pairing with. I was at the command line typing and found myself hitting <tab> repeatedly, hoping it would complete with the authors I pair with most often.

So I quickly whipped up a zsh completion script, and boom, pairing nirvana.

To use the completion script, save the following script as _hitch and add it to your fpath:

If you see any way to improve the function, please fork the gist on github.

QR Codes in Ruby

QR codes seem to be everywhere these days. I've seen them in advertisements on the train, on the back of magazines, and, most recently, on someone's wedding invitation. A lot of folks are using QR codes as a quick way to share URLs. But that's not all that they are good for. We recently had a client that needed to authorize paper tickets, and a QR-encoded token turned out to be a great solution.

QR Code Generation in Ruby

From my experience, there are two solid options for QR code generation in Ruby:

  • rQRCode Pure ruby QR code generator
  • qrencoder A Ruby wrapper around libqrencode

rQRCode

  • Pure ruby
  • Portable: will run on many Ruby platforms
  • Few dependencies
  • Relatively slow

QREncoder

  • Very, very fast
  • Uses a C library (libqrencode) that works wherever make does
  • Likely not to run on JRuby

QR Code Decoding in Ruby

The QR Decoding landscape varies even more than its QR encoding counterpart. There are several ooptions to choose from, and the best one for you will likely depend your environment and stack. The top three libraries that I've found are:

  • qrio Pure ruby QR Code decoder
  • zxing.rb JRuby wrapper for ZXing 1D/2D barcode image processing library.
  • qrdecoder A ruby wrapper for the C++ port of ZXing

Qrio

  • Pure ruby
  • Source is readable for ruby developers
  • Has intermediate hooks for displaying pattern detection
  • Only reads in PNG formatted images
  • Slower than the other options

Zxing.rb

  • Uses the well-maintained Zxing (Zebra Crossing) Java barcode decoding library
  • Works with both JRuby and MRI (via jruby-jars gem)
  • MRI versions of ruby will notice a slow startup as a forked JRuby server is started

QRDecoder

  • Wrapper for libzxing, a C++ port of the ZXing Java library
  • By far the fastest option for MRI
  • The C++ port of ZXing is less maintained than its big brother
  • You must build libzxing from source (a homebrew package is pending)

The Bottom Line

Wherein I offer "My Two Cents". If you're on a stack that can install libraries and you're using an MRI ruby (1.8 or 1.9), use qrencoder and qrdecoder. They are both very fast and awesome. Additionally, their APIs complement each other. If you're on Heroku, use rQRCode and qrio. They're both pure ruby and play nice with an environment where you can't install you're own libraries. If you're on JRuby, use Zxing.rb for decoding, and try both rQRCode and qrencoder. I haven't tried qrencoder with JRuby–or any compiled gems for that matter–so your mileage may vary.

N.B. In the interest of full disclosure, I have contributed to both qrencoder and zxing.rb, and maintain qrdecoder, and tend to favor them. Nevertheless, the pure ruby options are solid and well worth a look.

Regardless of what your needs are, there are lots of options. Which one fits your needs best will largely depend on your stack.

VIM script to toggle do/end and brackets

This is still rough, but I'm finding it quite useful:

I've converted this to a full-blown VIM plugin: http://github.com/jgdavey/vim-blockle

Rails migrations - Checking the existence of a table

Recently, I had to drop a table because I no longer needed the model at all. However, working on a distributed team, there were other folks with varying levels of "migration" status for their databases. A first migration to drop the table would be simply the following:

However, I also wanted to get rid of the original migration file that created the file to begin with, since it was no longer meaningful. As it turns out, checking the existence of a table isn't complicated at all:

Now, folks who never had that table to begin with won't get an error when running rake db:migrate, and those who did will simply drop it. This works because connection is actually delegated to ActiveRecord::Base.connection.

As a disclaimer, I've only used this technique in Rails 2.3, so I can't vouch for it working in other versions.