Ruby and RFC2822 after midnight

Updated 21Dec2004 23:35

I've been playing with Ruby of late, gradually getting my head around some of the more interesting features such as Blocks and Iterators to do something interesting with RSS. I'm not going into details, but let's just say that once it is complete, it should make things a bit more interesting on this blog.

Anyhu, part of my work tonight had me playing around with the DateTime class. One thing I found missing was a simple way of pumping out a date in RFC 2822 format (think PHP's date('r') function, and most dates on this site.)

One of the best bits of Ruby is this :

require "date"

class DateTime
    def to_rfc2822
        sprintf("%.3s, %02d %.3s %04d %02d:%02d:%02d %s", 
				Date::DAYNAMES[self.wday], 
				self.day, Date::MONTHNAMES[self.mon], 
				self.year, self.hour, self.min, self.sec, 
				self.zone)
    end
end

What have I done there?

I've taken the existing DateTime class, opened it up again, and added a new method to it called to_rfc2822. The code is trivial, but demonstrates one of the strengths of Ruby - third-party libraries are never closed.

Incidentally, I've found working in Ruby a pleasure that I haven't experienced in a long time. The code is quick, clean and concise. Blocks and iterators feel so much more natural once you begin using them, and the flexibility of the built-in classes (such as the Array class) often makes refactoring trivial (or at the very worst, a great deal easier!). Code reads more like English than anything else, with phrases like

puts "Line of debugging here:: x=#{x}" if @debug

or

@items.find { |item| item.name='test' }

making it easier to keep as much code floating around in my limited short term memory as possible!

Anyhu, keep your eyes peeled for the new features I've got up my sleeve for this site.

I've also been told by a friend that the colour scheme needs an update. At the risk of being one of those 2-week-per-style sites, look out for that too over Christmas. Yes, comments and suggestions are always welcome.

Later that day :

Typical.

It seems that I didn't have to even write that rather dashing sprintf call. Apparently, although the DateTime class doesn't have the rfc2822 method, the Time class does.

I'll have to look into whether DateTime inherits it from Time maybe, although the documentation for DateTime suggests it's a subclass of Date.

The mind boggles at that one...