Thursday, February 21, 2008

[Rails Cookbook Recipe 10.2] Fixing Bugs at the Source with Ruby -cw

Using the syntax checker is a great way to make sure you're supplying Rails with valid Ruby, and it's easy enough to do every time you save a file. If you're using any modern programmable text editor, you should be able to check syntax without leaving your program. For example, while editing the solution's student.rb file in Vim, you can type :w !ruby -cw in command mode, and you'll see the following within the editor:


:w !ruby -cw
-:4: parse error, unexpected '}', expecting kEND
Student.find(:all).map {|s| s.age }}.flatten.uniq.sort
^

shell returned 1

Hit ENTER or type command to continue

If you're using TextMate on a Mac, you can set up a keyboard shortcut that filters the file you're working on through a command such as ruby -cw. If you're not using a text editor or IDE that offers this kind of flexibility, you should consider switching to something like Vim, TextMate, or Emacs, and learning how to customize your editor.

No comments :