Wednesday, May 03, 2006

StL.rb Hacking Night - Hacking the Ruby Quiz

Well, once again Craig, Sean and I met this past Tuesday for the weekly hacking night of the St. Louis Ruby Brigrade. We decided to work on the current Ruby Quiz which Sean had submitted. It was a lot of fun and a good idea for what to work on if there is no other ideas.

This week's quiz [1] concerned cat2rfb.rb which from the RubyQuiz site he says : "The goal of cat2rafb is to implement a command line utility that will allow you to submit arbitrary text to http://rafb.net/paste/ and get the URL for the text back." You have to understand what rafb.net/paste is all about. This is a site where you can post snippets of code or text then it redirects you to a page where your code is displayed and syntax colorized. But the cool thing is the URL can be shared with others to collaborate.

From their FAQ [2]: "What is a 'nopaste' site?

A 'nopaste' site allows people to paste chunks of code for others to view. This is useful for situations such as asking for programming help on IRC, where it is frowned upon to paste chunks of code to the channel or to individual. With a nopaste site, the user pastes his or her code to the site and is given a url to provide to others so they can find the code."

What Sean wanted was an easy command line way to paste the code from files, or stdin w/o having to go through the web site. Bonus points if you convert the resulting URL from rafb.net through rubyurl.com which is a Rails equivalent to tinyurl.com that converts long URLs to short ones. (Not that the resulting rafb.net code display URLs are that long anyway.)

Sean hadn't actually come up with an answer for his own quiz yet, so that made me suspect he wanted to prime the WHN attendees with some actual cool work to do. In any case, it was a fairly good learning session for me. We actually set up an IRC session on freenode/#stl.rb to share links. Eating our own dogfood, I guess.

The basic idea is becoming familiar with Net::HTTP posts and gets. Sean went for a Ruby Golf approach, while I took the opposite approach. I wanted to provide options and error checking and read from an optional config file. The fields on the nopaste form allow you to choose from different languages, choose a nickname and give it a description. This makes it easier to find in the Recent pastes screen.

The other tricky part is understanding how to follow a redirect from Net::HTTP#post. Turns out Net::HTTPResponse has a bunch of subclasses. So you can do a 'case when' around the response object and then query its 'location' key (it behaves like a hash.)

I also figured out the issues wrt using GetoptLong and ARGF together. You need to process the arguments first, then set a post argument to ARGF.readlines. Also, RDoc::usage is a seriously cool thing. It looks at the comments at the top of your file and processes them into an usage statement and then exits. How can it see the comments in a file that calls it? Very deep Ruby-fu there. But this fits nicely with the DRY rule (comments and usage decared just once.)

So, we finished up right when the meeting was supposed to end. This kind of exploration to solve the quiz was well suited to the amount of time allocated for a hacking night. Look on ruby-talk for mine, Sean's and other answers to this week's quiz.

[1] http://www.rubyquiz.com/quiz77.html
[2] http://rafb.net/paste/faq.html

4 comments:

Sean said...

I humbly submit that if you think I am on my way to Ruby mastery, you need exposure to some true masters. Frequently, these are long time Smalltalk masters, like Ryan Davis (zenspider), and they tend to do much more with so very much less code. I am barely on track to general competence.

Ed Howland said...

You see, when you are at, say, 1 foot elevation, and you look up and you see someone so far up the slope they barely register on your optic neurons, you just assume they must be a master. I can't even see anyone past you yet, so I'll have to assume what you say is true.

Someday, I hope to be where you are at now, so I can at least be aware of even more masterful practitioners of the art.

Anonymous said...

I have to agree with Ed. Sean is definitely past general competence regarding Ruby. Probably not on par with ZenSpider, but still up there, from our perspective. Heck, even the fact that he acknowledges that he still has much to learn shows that he's far along the path to enlightenment.

I don't think I'd heard the term "Ruby Golf" before. This email explains it a bit. Basically, it means trying to solve the problem in the least amount of code. (Although not necessarily so little as to make it unreadable.)

The biggest thing I learned doing the quiz with Sean and Ed is that Ruby 1.8.2 is missing a heck of a lot of functionality in the standard libraries. And the online docs do not tell you which version a given method was introduced. So I tried to struggle along with 1.8.2, but had to give up eventually. I've since installed 1.8.4, and everything is going a lot more smoothly now. (I'm running Debian 3.1, and trying to stick with standard packages whenever possible.)

Ed Howland said...

I first heard of Perl Golf in the now defunct printed version of the Perl Journal. I can't find an online archive of it, but I'l look it up. I think it was 2000 or 2001.

All I know for certain is, never challenge Mike808 to a game. You will be left standing at the hole 1 tee while he sips his brew at the 19th.