Saturday, May 10, 2008

Ruby Singleton Methods Handy for Quick and Dirty Debugging

In a previous post I talked about needing to dump ActiveRecord models in favor of record sets when producing large reports from a Rails application. While this works well, the record set returned when doing a select_all is an array of hashes. And, being a hash, if you misspell the column name and type, for example:



nil will be returned and you will be none the wiser. Rubies singleton methods can quickly be used to detect such mistakes. Merely override the [] method with a method that will raise an error if the key can't be found:



By appending the singleton method override for [] you can quickly find out if you have made a typo somewhere in your view when specifying the column names of the record set.

Tunnel Behind a Firewall to Connect with rdesktop (or mstsc)

Ok, ok, so we really have a couple of Windows computer at work that are being used to serve up Rails applications. They are firewalled so they don't accept rdesktop connections over their public IP. We do, however, have a trusty Linux box running an ssh server that has access to these Windows boxes via our internal network. This makes it possible to work from home, restarting mongrels and such, by tunneling through the Linux server. First you need to set up the tunnel. This is done as follows:



That is, create a tunnel through linux.box.name.com that connects to the internal network computer 10.0.6.13 on port 3389. Here, I have also set up the local port to be 3389 because it is the default rdesktop port and thus I have one less thing to specify to the rdesktop command. After this tunnel is set up you can connect simply:



To do this from a Windows machine you will need to install cygwin or putty to set up the tunnel and of course the command to connect is then mstsc.