Try is brilliant

Every day the power of the Ruby programming language amazes me. It makes programming so much simpler. Example.

Recognize this?

@person ? @person.name : nil

Of course you do. It’s common fare since the days of C. Well, I am here to tell you it’s an ugly construct. Here’s the same thing in “idiomatic Ruby”:

@person.try(:name)

So simple. Thank you, Chris @ Github. :try will be included in Rails 2.3 as well.

  1. No comments yet.

  1. No trackbacks yet.