Post-mortem debugging

Posted on December 20, 2006

ruby-debug 0.5.1 has been released! The installation as usual:

    $ sudo gem install ruby-debug

Below is the list of changes:

Post-mortem debugging

First of all, what the heck is post-mortem debugging? Let's say you run a script and instead of the expected result you get an exception trace. Wouldn't it be great if we can roll back to the point where this exception is raised and explore the state of your program (possibly, by moving up and down the frame stack)?

Note that it is different than setting a catch point. By setting a catch point you activate the debugger when an exception (of a specific class) is about to be raised and it doesn't matter whether it's going to be handled later in the code or not. In the post-mortem case you know that an exception's been raised and not handled as expected.

Now I'm going to demonstrate several ways of using this feature, starting from the simplest:

Google Calculator

Posted on December 16, 2006

Google calculator is a very powerful tool. Besides basic arithmetic, you can use it in many other interesting ways. But I always wanted to use it from the command line without opening a new Safari window.

Ruby to the rescue:

    $ cat gcal 
    #!/usr/bin/env ruby
    %w(rubygems open-uri hpricot erb).each {|lib| require lib }
    doc = Hpricot(open("http://www.google.com/search?q=#{ERB::Util.u(ARGV*' ')}"))
    puts (doc/'/html/body/p/table/tr/td[3]/font/b').inner_text

    $ ./gcal 2^20
    2^20 = 1 048 576

    $ ./gcal 'sqrt(-1)'
    sqrt(-1) = i

    $ ./gcal 30 rubles in dollars
    30 Russian rubles = 1.1379844 U.S. dollars

    $ ./gcal 2 gallons in liters
    2 US gallons = 7.5708236 liters

It's not too bad for three-liner like this. By the way, did I say that Hpricot is awesome?