Wednesday, January 31, 2007

Rails migrations and model changes

On our current project we use Rails migrations for table creation, table modification, and data initialization. Our migrations worked well enough when they were run incrementally through the course of normal development, but failed (seemingly intermittently) when running rake migrate against an empty database. However, running rake migrate again (picking up from where the failure occurred) seemed to magically solve the problem.

Magic in software is not a good thing. It indicates either ignorance in the programmer or bugs in the software. Fortunately this was just a case of ignorance. Our problems were:
  1. Failure to use ActiveRecord's reset_column_information after a migration modifies a table (or before a model is used in a migration).
  2. Failure to periodically test migrations from scratch.
Have you tested your migrations on an empty database? Do it now, rather than when you're trying to deploy your system for the first time.

For details, see the migration documentation here.

Wednesday, January 17, 2007

Brain Repair: Thinking in Ruby instead of BASIC

Brain Damaged

When I see an IF statement in any programming language, it still comes with a THEN. A FOR statement still comes with a NEXT. My first language was BASIC (learned on a Commodore PET in Junior High), and it's still in my head. As Edsger Dijkstra said "It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration." I believe that most of my mental mutilation has been repaired, but some residual damage is apparently there.

In the same essay, Dijkstra also says "The tools we use have a profound (and devious!) influence on our thinking habits, and, therefore, on our thinking abilities." Since switching to Ruby and Rails for the majority of my programming work, I have repeatedly found this statement to be true. Other than some long-ago assembly language programming, my past work has been done in a relatively similar family of languages (C#, Delphi, VB, etc.), causing me to settle into predictable and comfortable thought patterns.

Brain Repair

To dive deeper into Ruby and change some old habits I've been exloring the following with satisfying results:
  • Array and Ennumerable methods: (in the Pickaxe book or here). Arrays include the methods of Ennumerable, so both are worth knowing. Since ActiveRecord returns arrays for multiple record searches, it is good to know more than for and .each to work with the models.
  • Ruby modules: It's beautiful to have something other than inheritance to share behavior between classes.
  • Method parameters: Default parameters, hashes as parameters, variable length argument lists, using a code block as a parameter, using a code block at the end of the method call.
  • Dynamically adding methods to classes and objects.
It is productive to examine the Rails source code, scaffold-generated code, code from plugins and engines, etc. line by line. When I pick someone elses code apart, making sure I understand every method, operator, etc. used, I learn a lot -- fast. It is painful but effective.

Wednesday, January 3, 2007

Ruby now at number 10 on the TIOBE Programming Community Index

Ruby has moved up to position number 10 on the January 2007 TIOBE Programming Community Index (with a rating of 2.519%).

The next 3 positions on the list above Ruby are Javascript (at 2.845%), Python (at 3.502%), and C# (at 3.521%). Interestingly enough, the only language on the list in the top 10 that moved down compared to a year ago was PHP, having dropped from position 4 to position 5.