Ruby QR Code Generation

2D barcode standards allow relatively large amounts of information (~7000 digits for a QR Code) in a small footprint. In addition, their unit cost is negligible compared to RFID tags so instead of just gracing the exterior of foodstuffs and UPS deliveries, they’re finding a host of wider applications in supply-chain management, advertising, e.t.c.

Media_httpuploadwikim_qgbel

I recently had to develop an internal letter tracking system for a client, so thinking QR Codes would be a cheap and quick solution to the problem of tracking real-world items, I started investigating Ruby implementations.

To start with, there’s a ruby port of libqrencode and the rQRcode gem which generates a text version of the code. The rQRcode gem generated 100 tags in approximately 50 seconds, not really quick enough to use in production.

My stopgap solution was to compile libqrencode:

and then just issue a system call in ruby when needed:

Generating 100 tags took just over 2 seconds this way. The system being developed just required a timely way of being able to pull the correct tag off the file system, so a 50x speedup was sufficient.

I may well post more on this in the near future; From what I’ve RTFM’ed it would be fairly trivial to write a ruby extension based on qrencode.h.

OS X- Specific Shell Tools

One perk of working on a mac over other *nix variants is the tight integration with many of the higher-level os-specific features that make apple machines so nice to use. Here are a few you might not be aware of:

open

Pretty much does what it says on the tin. Pass it a file or directory. It’ll either open with the default application, or open up a finder window for the latter.

mdfind

Hit the spotlight index through the command line. I have an alias for “mdfind —onlyin . $1” for finding, say, a method in a big ruby project I don’t know very well. It’s a hell of a lot quicker than textmates’ command+shift+f search which freezes with alarming regularity.

say “something”

Text-to-speech tool. Pass it a string, and your wise words are reiterated. Use this while ssh’ed into a mac near you to convince the user they have Alzheimer’s. This is particularly effective when used in conjunction with “osascript -e ‘set volume 25’” to turn their speaker volume down beforehand. For even greater impact, run it as a cronjob.

osascript -e “script”

Inline evaluation of AppleScript. With Apple’s OSA you get a pretty expansive command set for controlling system behaviour, the finder, standard apps like iTunes. Try firing up the script editor utility and loading an application dictionary. I set the alias tmreload=”osascript -e ‘tell app "TextMate" to reload bundles’” to quickly reload textmate’s bundle list. For the rubyists, there’s an Apple-sponsored RubyOSA gem as well.

pbcopy

Pipe your output into pbcopy, then paste it into native Cocoa applications.

opendiff file1 file2

Fires up FileMerge, diffing the two files you passed as arguments.

dns-sd -R ‘share_name’ service_type . port extra_arguments”

Announce a service using mDNS. A list of service types is maintained here.

I’ve neglected to talk about things like launchd and the plethora of command-line tools adorned on OS X Server Administrators.

† I’m an OpenBSD purist; why complicate process management anyway?