Rescuing Ruby

Posted by admin 11/08/2007 at 19h07

Now, before anyone goes crazy, this article has nothing to do with saving ruby from something, i just couldnt think of a better title. We will be talking about rescue and some cool tricks that can be done with it.

As most of you vets know, every thing in Ruby is an object. What most noobs dont realize is how powerful that can be and how concise your code can become. Of course, the flip side of that problem is what i call, newbie-come-expert. It’s basically the stage in a noob’s study of a language where they think they know enough to write really cool software. What actually happens is they end up with code like this:

@objects.map {|e| [e.id, e.title]}.sort {|a, b| a.last <=> b.last}.map {|e| e.join(" ")}.join("\n")

Lovely.

Dont worry noobs, everyone does it. I’m still trying to undo it from some of my older code.

Anyway, my point is, sometimes Ruby’s dynamicism (say that 3 times fast) can lead to messy code so use this tip wisely.

On to the actual tip.

So, like I said before, EVERYTHING in Ruby is an object, even the remains of a rescue clause or an inline rescue which is what we will be playing with here.

How many of you have done this:

@person = Person.find(some_id)

only to get the following error:

ActiveRecord::RecordNotFound: Couldn't find Person with ID=22

It’s pretty annoying, mainly because this type of thing could easily happen in a live app. A user goes to find something that isnt there. One way around it is to have a rescue at the end of the method, but what if you have multiple finds that could throw errors? What if you still need to be able to proceed with whatever you are doing, even if the object isnt found?

This is where inline @rescue@’s can help.

Now you can pull all of your finds together and end them with a rescue.

Example:
Let’s say you had the following code:


@person = Person.find(params[:id])
@employee = Employee.find(params[:employee_id])

Now, if either of these finds fail, you will get an error. Instead of rescuing the entire method, you can resuce each statement and make the return value nil and check for nil like you normally would.

New code:


@person = Person.find(params[:id]) rescue nil
@employee = Employee.find(params[:employee_id]) rescue nil

Now when you cant find the ID, it will rescue and just set the variables to nil and move on.

You can also set the rescues to another default datatype instead of nil.

Now, you may be saying, “Wait, why would a user need to enter an id”. Well, one example is an order id, or a tracking number. Anyway, I have run across this a couple of times and thought i’d write something about it.

until next time.

—jake

This entry was posted on 11/08/2007 at 19h07 and Posted in . You can follow any response to this entry through the Atom feed. You can leave a comment or a trackback from your own site.

Tags , ,


Trackbacks

Use the following link to trackback from your own site:
http://blog.flvorful.com/trackbacks?article_id=2

Comments

Leave a comment