Log in

Archive for the ‘Coding’ 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.

Sep
11

Filed under things i’m not sure how i lived without, the Copy as RTF bundle for TextMate is absolutely amazing. Between work and our local ruby brigade (757.rb) i’m always copying little snippets of code into Keynote or emails, or something, now i get to keep my syntax highlighting instead of formatting by hand (ick!) or pasting screenshots of code (also ick!). Yeah, it’s kind of old news but still figured i’d throw it out there since it deserves some recognition. Thanks Dr. Nic!

Jul
18

A common problem i have on Twitter is trying to find out who exactly the people friending me are when they have large numbers of contacts. So i wrote a simple little script to filter out anyone on their friends list who i’m not a friend with. It’s not that pretty as i wrote it in a few minutes, but it gets the job done. Feel free to make a bookmarklet or GreaseMonkey script out of it just don’t forget to change the username.

var username = 'skhisma';
var url = 'http://twitter.com/' + username;
new Ajax.Request(url, {
  method: 'get',
  onSuccess: function(transport) {
	  // Find my friends
	  var myFriends = [];
	  transport.responseText.scan(/<span class="vcard">\s+<a href="http:\/\/twitter.com\/(\w+)\/?"/, function(match) {
		  myFriends.push(match[1]);
	  });
		// Hide their friends that aren't my friends
		$$('span.vcard').each(function(element){ 
			user = Element.childElements(element)[0].href.sub(/http:\/\/twitter.com\/(\w+)\/?/,'#{1}');
			if( myFriends.indexOf( user ) == -1 ) {
				element.hide();
			}
		});
		$$('div.section-header h1').each(function(element){
			if(element.innerHTML == "Following")
			  element.innerHTML = "Common Friends";
		});
  }
});