Ruby Substrings and Testing Legacy Code
Recently Josh Cronemeyer and I were working on writing a game in Ruby. Gosu , a 2D game library for Ruby and C++, and Chipmunk , a 2D Physics engine, do lots of the heavy lifting so we thought it would be a fun Saturday afternoon thing to do. However, the examples had no tests so when we tried to change some stuff, and of course it didn't work, we were all sorts of clueless as to why. Well, wrapping tests around legacy code is not a lot of fun but it is an effective way of debugging. Here's how I like to do it: Step one: Identify potentially troublesome code (As in "What the hell is that doing?"). Step two: Write some tests that verify the functionality of the code. Step three: Pull out the offending code into a method or methods. There's a fair bit of interplay between steps two and three, testing one piece may require extracting it. Step four: Either you've found your problem or you've accomplished four things: 1. You really understand the troublesom...