Log in

Archive for the ‘Ruby’ Category

Oct
23

Wanting to have easy access to all the documentation for my installed gems i decided to setup a startup item for the gem server that comes with RubyGems.

Come to find out the process is incredibly simple. First create a shell script to star the server:

#!/bin/sh
/usr/bin/gem server --daemon

Then create a plist file describing the startup item:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
    <dict>
        <key>Description</key>
        <string>Ruby Gems Server</string>
        <key>OrderPreference</key>
        <string>Late</string>
        <key>Provides</key>
        <array>
                <string>RDoc Server</string>
        </array>
    </dict>
</plist>

Create a folder, i named mine GemServer, and name the shell script the same. Put the script and the plist into this directory and copy it to /Library/StartupItems (you may have to create this).

Or, you could just download the startup item and extract it to /Library/StartupItems.

Restart and point your browser to localhost:8808 and peruse your documentation ’til your heart’s content.

Sep
25
Ruby is easy to read.

When i was first learning Ruby i, like most people, picked up two books. The first was the wonderful pickaxe book; the second, the no-less-wonderful Poignant Guide to Ruby by everyone’s favorite mad scientist – Why the lucky stiff.

When virtually flipping through why’s lovely creation one of the things that caught my eye was a a statement early on in which he simply asked us readers to “read the following aloud to yourself” and followed it with a small block of ruby code:

5.times { print "Odelay!" }

I, being in the comfort of my own home late at night, obeyed and read “five times print odelay!”. I chuckled and moved on. But he persisted asking that i then read another block of code:

exit unless "restaurant".include? "aura"

And once more i obeyed chuckling even more at the concept of a restaurant aura. I’ve often used this as one of the reasons why i love Ruby. When written right it just sort of makes sense. You can read it and understand it more-often-than-not. It’s not the insane business speak like COBOL or the drawn-out run-on sentences of Java, but just simple little statements that do their best to not confuse the reader.

TextMate's Speak Selection option.

Why is all this interesting? Well i was wandering about the bundles menu in TextMate today and stumbled across the ability to Speak Selection. I hastily highlighted a line of the Javascript library i was working on and hit Speak Selection. What i got was something between a three-year-old telling you about their day and a speech synthesizer being circuit bent. I copied in Why’s first example and did the same and was rewarded with “five dot times print Odelay!”. Not exactly how i had read it but still mostly understandable.

So this begs the question: could ‘speakability’ be a viable way to determine if code is understandable? If you can actually read your code aloud – and have it explain in more-or-less english what it is attempting to do – it would seem that there is a much greater chance that the next person to look at your code will be able to interpret it as well.