21st May 2002

It’s been a very busy month, I can tell you.

Ruby/Google is at release 0.4.0 now. This is a beta release and will probably be the last for a while. All the useful features that I can think of are now present and, to the best of my knowledge, functioning correctly.

bash-completion continues to do well. It’s in Debian and Mandrake‘s Cooker release, but was pulled from Red Hat 7.3 for the fourth beta. Oh well…

I’m currently working on Ruby/DICT, which will be a client and client-side library for the DICT protocol, as defined in RFC2229.

I really can’t speak highly enough of the Ruby programming language.

Just look at this piece of code:

module HTML
  def method_missing(methId, data, attrs={})
    tag = methId.id2name
    tag.upcase!
    attr_str = ''
    attrs.each do |key, value|
      attr_str << sprintf(' %s="%s"', key.upcase, value)
    end
    sprintf("<%s%s>%s</%s>", tag, attr_str, data, tag)
  end
end

Assuming that has been saved as html.rb, you now have a very simple module that returns strings marked up as HTML, just like CGI.pm in Perl.

You call it like this:

#!/usr/bin/ruby -w
 
require 'html'
include HTML
 
puts a('Google', 'href' => 'http://www.google.com/')
puts ul(li('item1') + li('item2') + li('item3'))

The output looks like this:

<A HREF="http://www.google.com/">Google</A>
<UL><LI>item1</LI><LI>item2</LI><LI>item3</LI></UL>

Voila, no more arsing around with HTML tags in your scripts.

This is how it works. You simply call the tag that you require as if it were a method in the module HTML. However, no such method is defined, so Ruby invokes the method method_missing to handle the error. By default, this method is, itself, undefined, which leads Ruby to raise an exception.

However, in the module above, the method has been defined. As its argument, it is passed the method as a symbol name, then the original arguments to the non-existent tag method. The last argument is optional and is a hash of attribute/value pairs, common in so many HTML tags.

The method then converts the tag and attributes to upper-case, formats the tag around its content, and ultimately returns it to the calling code.

The thing that’s remarkable about the above code is how brief it is and how easy it was to write. It took me all of five minutes. Consider how the same code would look in Perl or Python.

Sarah and I are leaving for a long weekend in Vancouver, Canada on Friday. I’m rather looking forward to that. It will be another nice break, probably our last in the run-up to the wedding in August.

It looks like we’re going to have to move. Our landlord wants to sell our apartment and we’re not about to cough up the $425,000 he wants for the place, so we’re going to be on the move again.

I think we’re going to end up in Menlo Park this time. We’ve seen a couple of really nice places and I hope to sign a lease later this week.

It’ll be a shame to leave this place. I really like Palo Alto and being close to University Avenue has been fantastic, but there just seems to be a dearth of large apartments in this town. Menlo Park’s just up the road, though, so it’s not like we’ll be travelling very far.

This entry was posted in Advogato. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *