Light in Canterbury

13 August 2006


Light
Originally uploaded by Tom Armitage.

I went to Canterbury last weekend. We were hungover, one Sunday morning, and an advert came on TV telling us that Canterbury was really quite close and really quite cheap. We were desperate for a break from the city, so we piled over to the tiny little city for a day. Very pleasant, too; a break from the strain and strife, which were replaced with wandering, food, drink, and shopping. I also took my camera, and the results are now on Flickr. Some of my favourite pictures with it so far; this one, with no modification bar some sharpening, levels and channel mixing, stood out.

uptime

13 August 2006

I haven’t rebooted my computer in a long while.

It all happened when I started preparing for ETech, and became irrationally concerned that my elderly Powerbook might never turn on again. So I kept it up constantly, for a few months.

Then I had loads of stuff after that “up”, floating around, and it was taking ages to process. So I didn’t reboot.

Then it was Reboot, and again, I didn’t reboot in case it wouldn’t come back and I lost my work.

I think the paranoia had set in.

Anyhow, GeekTool sits on the bottom of my screen, telling me all about my uptime. I looked just now, and it said

13:24 up 212 days, 3:32, 5 users, load averages: 1.50 1.61 1.35

5 users? Not sure what that’s all about. Anyhow… 212 days is a long while. I think we need a spring clean. Time to restart.

So, in response to an earlier post, “mexx” asks:

Let’s say that I have a class BigTree and I have a string ‘big_tree’ how do you get the string translated to ‘BigTree’ so that I can use this translated string the way you showed?

That’s a fun challenge. Well, as I’ll later show, it’s a problem to which I already know the answer. But I gave it a shot, and came up with this – my first genuine, off-the-top-of-my-head one-liner (and I wasn’t golfing):

def camelize(str)
  str.split('_').map {|w| w.capitalize}.join
end

That’s a bit cryptic. To decode it, if Ruby’s not your first language:

  1. Take a string
  2. Split it on underscore characters into an array
  3. Capitalize each item in the array in turn (which I do by mapping the array to a new array, with the built-in String method capitalize)
  4. …and then join the whole array back into a string

Unfortunately, that works for mexxx’s example – big_tree will map to BigTree but it’s certainly not true for all possible classnames.

But, as I said earlier, I already know the answer to this. It’s part of Rails – specifically, it’s in the Inflector component of ActiveSupport. Go to the API docs and look for “camelize”. Most of the Inflector components are dependent on each other; camelize, fortunately, is only dependent on itself. And it looks like this:

def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
  if first_letter_in_uppercase
    lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
  else
    lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1]
  end
end

That looks a whole lot more convincing and generic. And I think that’s your answer, mexxx. So don’t thank me – thank the Rails core team. There’s lots of useful basic Ruby tools hidden in the Rails sourcecode – it’s always worth an explore, if only to find out more about how it does its stuff.

To cut a long story short: the slides for the talk I gave earlier this week are now available. You can find out more about the talk on the talks page of this site, or you can download the PDF (1.5mb). It should be fairly self-explanatory.

(A brief summary for those of you unable to scroll or click: it’s a client-side-developer’s perspective on Rails, and how to integrate client side development into the build process).

I’m going to be speaking tonight at LRUG. The talk is called “Ruby on Rails from the other side of the tracks“, and it’s about how client-side developers fit into Rails, and how you (as a back-end developer) can work with them rather than against them. If that sounds interesting (or, more to the point, you want to hear Tiest talk about Domain Specific Languages, which should be great), do come along.

Didn’t notice this when it happened (because it didn’t necessarily appear on the frontpage) but… my review of David Black’s Ruby for Rails has now gone live over at Vitamin.

My first piece of technical writing. It’s a good book, too – clarified an awful lot about the hierachy of classes within the language, and explained the nuances of Ruby dynamism very well; strongly recommended to anyone coming to Ruby (through Rails) afresh, whether you’re an experienced programmer or not. I hope I conveyed that in the review.