Friday, February 13, 2009

Printing exceptions in Ruby

Steve wrote a post about the simplest way to print an exception's message and backtrace in ruby.

The snippet he provides is the following:

# catch most exceptions (anything that derives from StandardError)
begin
...
rescue
puts $!, $@
end

# catch all exceptions (anything that derives from Exception)
begin
...
rescue Exception
puts $!, $@
end
end


The $! variable contains the last exception and $@ contains the last exception's backtrace as a string.

Some people (including me, before reading his post) do the following:

begin
...
rescue Exception => ex
puts ex.message
puts ex.backtrace.join("\n")
end


I'm glad I stumbled upon Steve's post. Learning about the little idioms of a language helps you to write more succinct and clear code.

Wednesday, February 4, 2009

git: prune to remove old remote tracking branches

I was trying to delete "somebranch" from a git repository today but got this error message, which baffled me for a bit:


$ git push origin :somebranch
error: unable to push to unqualified destination: somebranch
The destination refspec neither matches an existing ref on the remote nor
begins with refs/, and we are unable to guess a prefix based on the source ref.
fatal: The remote end hung up unexpectedly
error: failed to push some refs to 'git@my_server:my_repo.git'


Doing a 'git branch -a' showed a "origin/somebranch" remote branch. Why can't I delete it? Then I realized that the branch might have been deleted on the remote repository and I haven't updated my remote tracking branches yet. Doing a "git pull" won't remove remote tracking branches for branches that have been deleted. To do that for a remote named "origin", you'll need to use this command:

$ git remote prune origin

Tuesday, February 3, 2009

pv (Pipe Viewer) - a unix utility for monitoring the progress of data through a pipe

Just learned about Pipe Viewer from this blog post:
Pipe viewer is a terminal-based tool for monitoring the progress of data through a pipeline. It can be inserted into any normal pipeline between two processes to give a visual indication of how quickly data is passing through, how long it has taken, how near to completion it is, and an estimate of how long it will be until completion.

Here is the example that Peteris provides. For long-running commands, it gives you a progress indicator so you won't be left wondering, "Should I go get lunch, or wait for the gzip to be finished?"

$ pv access.log | gzip > access.log.gz
611MB 0:00:11 [58.3MB/s] [=> ] 15% ETA 0:00:59

Sunday, February 1, 2009

If you installed any of the Ubuntu flavors (Ubuntu - default with the Gnome environment, Xubuntu - a flavor that uses the Xfce desktop environment, or Kubuntu - a flavor that uses the KDE desktop environment), you can switch between them without their respective install CD's by installing the right set of packages.

I recently ran into this because I used wubi to install Kubuntu, but found that KDE was a bit "busy" for my liking. (I have a short attention span, having more things on the desktop ended up being distracting for me.)

To install Ubuntu desktop:
sudo apt-get install ubuntu-desktop

To install Xubuntu desktop:

sudo apt-get install xubuntu-desktop

To install Kubuntu desktop:

sudo apt-get install kubuntu-desktop

If you're settled with switching from one environment to the other and would like to remove all the packages from the one that you no longer use (for example, xubuntu-desktop), do the following:

sudo apt-get remove xubuntu-desktop
sudo apt-get autoremove