Sunday, April 09, 2006

Playing with Ruby on Rails

Recently I decided to play with Ruby on Rails and see what all the fuss was about. I've been around a while and taught myself a bunch of stuff along the way from Omnis7 to Java to T-SQL to .NET, and so Ruby on Rails shouldn't be too hard ... right?

Well, it wasn't, once I invested in Programming Ruby and Agile Web Development with Rails. Those two books helped me considerably, especially the follow-on tutorials in the Rails specific one. Without those I would have been left floundering.

However, there were two problems that neither book really helped me with (although the first one was properly shown in the Programming Ruby book, it just didn't bother to point out the weirdness of it).


You might not have realised it, but that heading above is missing an "e" (and a space, but most programming languages ignore that). Unlike any other language I have learnt, Ruby's If ... Elseif ... End construct spells the "Elseif" without the second "e", as in "Elsif". Perhaps it was because Matz is Japanese, but this really threw me - I even ignored the way the Ruby book showed me it was.


Rails' ActiveRecord is very nice for doing lots of hard work for you. Thank you very much David Heinemeier Hansson. Unfortunately it maps decimal database columns to float variables ... which if you have ever tried using floating numbers in financial calculations you would know means major problems!

Frankly this is a show-stopper for me. I can't imagine ever doing something real with Rails that would not involve dollars, and I certainly do not want nasty rounding errors creeping in because there are floats mapped to my decimal table columns. It turns out that the solution in Ruby is to use the BigDecimal data type. It took a little while to find the API documentation for this datatype, but eventually I did.

That didn't help me work out how to implement it. The Rails community is full of talk about modifying ActiveRecord to do the job properly - but it turns out that ends up involving a lot of database-specific code, and whilst ProgresSQL and SQL Server have been done, good old MySQL is left wanting, and that is what I am using to play around with. There is a little blurb in the book about overriding the accessor/mutator attributes for a model (don't worry if that makes no sense to you, it didn't to me either until about 8 hours into the tutorial). The online API documentation for ActiveRecord seems to hint that the _before_type_cast accessor could help here.

After too much mucking about, I finally got an answer that is elegant and works very nicely. I even have some sample code for those other newbies following the Rails tutorials. At the beginning of our Product model we need to add a couple of lines:

# modified this to get the price as a decimal

require "bigdecimal"
require "bigdecimal/util.rb"

Then later, we can override the default price accessor/mutator with a simple call to BigDecimal:

def price
    # we want to make Price a BigDecimal, the format
    # will look weird if we don't do anything to it
    # but we are using sprintf() in our fmt_currency
    # method, which handles this nicely
    BigDecimal(price_before_type_cast, 2)

As I've noted, the main problem with BigDecimal is that the native display of it is ugly. A value of 20.00 is displayed as 0.2E2, which is nasty. Fortunately the formatting seems to work out in text fields OK, and we end up formatting price everywhere we show it as a label so that it shows with dollar signs. The formatting function sprintf() has no problem with BigDecimal so that works fine!

So far (and I have all of the cart code working) this does not seem to lead to any issues with the Depot tutorial, and it gives an elegant solution (provided you are willing to create a formatting method for displaying these numbers wherever they are outside form fields).

[EDIT: The year-old enhancement request relating to the decimal issue is an interesting read.]

Submit to Reddit

Friday, April 07, 2006

Total Solar Eclipse from Space Station

Here is what a total solar eclipse looks like from space, courtesy of the astronauts on ISS, the big black mark is the Moon's shadow moving across Earth's surface:

Image Science and Analysis Laboratory, NASA-Johnson Space Center. 6 Apr. 2006. 'Earth Sciences and Image Analysis Photographic Highlights.'

It kinda reminds me of one of those creepy moments in sci-fi/horror films when they realise that the "black nothingness" is spreading. That is one of the cool things you get to see when you have people permanently in space.

Will Australia get into space?

Saturday, April 01, 2006

Falkayn's List of Web 2.0 Lists

For various reasons I've been exploring the Web 2.0 world more lately than previously. I have come across a few lists that I thought I would share, in case you're looking to learn more about this yourself.

For a start you must read Tim Ziegler's Webmonkey article Web 2.0: A Pattern Library for an easy to read breakdown of Web 2.0. It's only a summary of features/attitudes, it will be out of date within a year (at most), but it's pure gold if you are still fuzzy about what is Web 2.0.

Bob Stumpel's Sacred Cow Dung has the first list of Everything Web 2.0 that we'll look at. It's a nice list, posted in a blog post and limited to simple categorisations and bullet point format. Micro descriptions of each one help you decide whether to hit their link or not.

Now for a different sort of list (or two). ProgrammableWeb is looking to get you to their site with a short but sweet list of Web 2.0 APIs, and a more detailed list that covers Web 2.0 Mashups. They have a pretty decent Web 2.0 reference page that references sites talking about Web 2.0, common technologies used by Web 2.0 sites (including Amazon links for books so they can make a buck off it).

The eConsultant has an impressively plain (looks very Web 2.0 itself) Web 2.0 directory that both categorises sites and gives you an alphabetical view of them. Like any categorisation method it is limited by one person's view of things.

So we come around to the more interactive and truly Web 2.0 lists. First up is the eponymously named Web 2.0 List. There is no About page on this site, but basically it seems to be creating a ranked list of Web 2.0 sites based upon Alexa, and digg. Or perhaps it's just from Alexa, I included the other two because a) you could, and b) they show links to all three after every listing. The link lets you tag that site (rather than searching for it), but the digg link just lets you search for it. The Alexa link shows you their Alexa stats page. Confusing (an About page would sure help!), but interesting, especially when you include a tag cloud, give registered users the power to vote for sites, and show lists like top voted and top rated. A fun geeky site and worth it alone for the ability to get a tag cloud view of various memes at work.

Of course some of their data is a bit wrong, for example FCKeditor is listed as a Web 2.0 site. Well, it isn't, not by a long stretch of the imagination. It is a very nice Javascript-based text editor widget that can replace TEXTAREA fields in your online forms, but Web 2.0 it ain't.

So let's say we stay with human-editing, but want something a bit more Web 2.0 than we've seen so far. The Museum of Modern Betas may be just what the DJ ordered. It is basically in a blog format, with simple entries that show the name of the beta site (not necessarily Web 2.0, but nowadays almost always is), a thumbnail of a screenshot and the date posted. Clicking either the name or the thumbnail will take you to a more detailed page that shows a little bit of detail about the beta, usually a quote from the About page (the Web 2.0 List entry is decidely small) and often links to press about the beta. They've gone over 1,000 betas and seem to have their finger on what is happening at the moment. Cool features of MoMB are:
Use all of them, use none of them, I don't really care. But I do hope this helps you get a few more hours of surfing Web 2.0 than you might have got on your own.

Feel free to comment on any I might have missed!

Saurier from MoMB kindly points out that Emily Chang's eHub is the most popular list around that I missed out (weird seeing as she's on the Web 2.0 Workgroup), and reminded me that Techcrunch have their own list of Companies and Products, which has the lovely feature of being available as an RSS feed (slurp!).