The Twilight Sun

Using retry after a LoadError

by Jonathan on May.12, 2011, under Ruby

Someone in the Ruby IRC channel pastied this code today and complained about how ugly it is:

  require 'vlad'
rescue LoadError
  require 'rubygems'
    require 'vlad'
  rescue LoadError

It’s meant to account for differences between Ruby 1.8 and 1.9, where rubygems is automatically loaded. And this code is pretty ugly. I came up with a simplified version that takes advantage of require()’s return value, and the `retry` statement:

  require 'vlad'
rescue LoadError
  retry if require('rubygems')
  # raise

The `retry` keyword jumps back to the start of the ‘begin’ block so you can try to load your libraries again. Used carelessly this would turn into an infinite loop (LoadError occurs, retry, LoadError occurs, retry, ad nauseum), but since require() returns false if the library was already loaded, you can skip the retry in that case.

This version eats the LoadError, which is useful for an optional dependency, but if you don’t want that you can comment out the `raise` above. If the retry ended up not doing any good, it’ll just re-raise the LoadError that was rescued.

Frankly, this probably isn’t very useful in practice. After all, if you cared about 1.8/1.9 compatibility, you could just require(“rubygems”) first and get rid of all of the exception handling. I think it’s a pretty good example of how `retry` works, though.

9 Comments for this entry

  • Jonathan

    I find it funny that I typed in and found an actual site. You have appeased my growing hunger for humanity to end its reign of horrid terror that stalks the city of Embers. I happen to think that this site is very swank. Swank as in sweet. Sweet as in Sugar. Sugar as in what dissolves in water. Water as in ocean. Ocean as in sea. Sea as in see. See me? I am Jonathan. Peace.

  • Jonathan

    Oh wait….wrong site. I thought this was…uhm… I take it all back. Peace

  • Johnathan (WITH AN H)

    Hey bro. Your website is pretty cool, I’m no programmer, but I do like technology and I currently work at a computer support desk at my college. I also noticed you’re a writer, just like me (aspiring, at least). I came here when I typed in (that’s my name, minus the “.com”). Anyway, are you willing to sell the domain? Lets be honest, you already have the, you don’t want to be not be selfish.

    If you don’t want to sell it, that’s fine.

  • Jonathan(2)

    I can’t believe all the Jonathans’ will be checking out this website!! :D

  • Jaredsexxy ramsey

    Thanks jonathan.Com…Am on fb jared ramsey

  • Johnathan

    Why name a website after your name blogging about stupid things half the people look at have no clue what in the geek your talking about I’m Johnathan and I after my name and get a thing on rubies and doing dishes wtf choose a tech geek website name maybe

  • paul

    Hi – Is the domain name for sale at all?

    Please name your price

  • Jonathan

    Hello, my name is Jonathan Bopp.
    I landed here because of the domain, and would like to ask you, if you would like to give it away. I see the last articles are from 2011.
    I readed your story and it’s nice that you learned to do sites, you can always put online things you want. Not everybody can do that ;)
    I do massages and heal through breath and touch, and was thinking about a domain that fits me to use for this activity and maybe other projects.
    If you want you can send me an email, I leave my adress in the description.

  • Jonathan

    Having known this domain since you had the fiery logo, I’ve always wanted Instead, I settled for my full name.

Leave a Reply


Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...