Give it a try()

March 2nd, 2008

As one of the designers at bragster, it’s a bit odd that my first blog post here is about Ruby, but we all get stuck in with all sorts in the bragster garage so here goes.

I should also note that at least for the time being we’re gonna keep this kind of ‘techy’ blog post off the main page and in relevant dev sections. That means they probably won’t show up in the main blog feed either, so if they peak your interest, you’ll need to subscribe to the feed for this section separately.

On to the topic at hand!

One thing that crops up again and again in the bragster codebase is attempting to display a bit of data that may or may not actually exist depending on the circumstances. The kind of thing where you normally end up using this:

user ? user.display_name : nil

Chris Wanstrath has come up with try() so that instead you can just do this:

user.try(:display_name)

Looks pretty handy and I’m gonna give it a whirl to see if we should start using it at bragster.

Sorry, comments are closed for this article.