Refactoring old Ruby code

24 May 2007

I recently started looking at an old project again - it's a project where I was translating some artificial intelligence code examples from C to Ruby. It's neat stuff - bigrams, simulated annealing, adaptive resonance theory, and so forth; and there's actually a second edition of the book available.

At any rate, it's funny how the code looks now. Some examples:

  • I was using Hash.new rather than just {}.
  • I had return statements everywhere.
  • Was using and vs &&.
  • Was using the curly braces for multiline blocks rather than using do..end.
  • Was using .to_s and + rather than just interpolating strings.

Most of the code was just a straight port from C to Ruby, so I didn't go back and polish it up - so there are still lots of places where the code is syntactically Ruby but semantically C. Maybe I can revisit some of that code - and relearn some of the concepts as well.