February 2005
[ Aslak Hellesoy ] 19:54, Saturday, 26 February 2005

Rich, responsive webapps using the Ajax approach are chic. I've started to think about what it would be like to develop the view part of a webapp using the Ajax concepts.

In an Ajax web app the server sends back XML, and the XML is transformed to HTML in the browser. There are several ways to do this transformation:

One approach is to use XSLTProcessor as described here.

However, XSLT is problematic for a couple of reasons. First, not all browsers support it, and more importantly, most web ui designers don't speak XSL.

A second approach is to use XPath in JavaScript to pull out bits of pieces of the XML and modify small pieces of the DOM to fill them in.

A third (and new?) approach could be to use the familiar template approach (as in JSP, ASP, ERB, PHP templates) and generate bigger chunks of DHTML. This would make it much easier to develop nice looking views, since they can now be designed in a regular web authoring tool such as DreamWeaver or NVU.

But wait - the template engine now has to sit inside the browser! And this is where I discovered JavaScript Templates.

JST templates could theoretically be rendered (and tested) in a build system, using Rhino. Being able to test view rendering is something I consider very important, especially when systems start to get complex (which they do with Ajax).

I'm eager to try this out.

[ Aslak Hellesoy ] 06:05, Friday, 25 February 2005

A couple of days ago I wished there was a way to use annotations in Ruby. I'm writing a Ruby on Rails application that renders a lot of objects in HTML. Many of these objects are "editable" via the web interface, and in order to make it easier for the user, I wanted the web interface to have both an explanatory text and a HTML tooltip for each object's field.

Because I like simple things I also like to keep things in one place (I don't want to maintain this metadata in a separate RHTML template).

Ruby doesn't support annotations out of the box, so I opened up the Class class and added support for annotations. Now I can do:

require 'rscm/annotations'

  class EmailSender
    ann :description => "IP address of the mail server", :tip => "Use 'localhost' if you have a good box, sister!"
    attr_accessor :server
  end

The class' annotations can then be accessed like this:

  EmailSender.server[:description] # => "IP address of the mail server"

It took the Java community forever to do stuff like this. First a couple of years of XDoclet, then JSR175. And tons of code too. In Ruby it's 1 hour's work and 25 lines of code. Go figure.

For more info see http://rscm.rubyforge.org/classes/Class.html

And oh, here's the code:

class Class
  def ann(anns)
    @@anns ||= {}
    def self.method_missing(sym, *args) #:nodoc:
      @@anns[sym]
    end
    $attr_anns ||= {}
    $attr_anns.merge!(anns)
  end

  alias old_attr_reader attr_reader #:nodoc:
  def attr_reader(*syms) #:nodoc:
    syms.each do |sym|
      @@anns[sym] = $attr_anns
    end
    $attr_anns = nil
    old_attr_reader(*syms)
  end

  def attr_accessor(*syms) #:nodoc:
    attr_reader(*syms)
    attr_writer(*syms)
  end
end
UPDATE (March 3 2005) An updated version of the Ruby annotations library is available at rafb
[ Aslak Hellesoy ] 02:08, Wednesday, 2 February 2005

Jim Weirich has written up an excellent compilation of what Javaheads should know about Ruby. I wish he had written up 2 years ago when I started to play with Ruby.

Java has been my language of choice for many years. Learning Ruby has opened up my eyes about what OO, agile development and extreme productivity should be like.

I'm not "converting" from Ruby to Java - I always try to use the best tool for the job at hand. Sometimes it's Java, sometimes it's .NET, sometimes it's Excel or PHP. More often than anything else it's not even about programming. But when it is, Ruby turns out to be the best choice for me more often than any other language. Probably because I'm an XP head. (And because I still don't know Objective-C or Smalltalk - it's on this year's TODO list).

Ruby - because I can get the job done fast and end up with super readable and maintainable code. And I'm worth it.

Now if those big corps that I tend to consult for would dare to do more things in Ruby... I reckon it will take a few years before we see them changing.