Friday, February 15, 2008

Tip of the day

I recently was working some OPC, that is, Other People's Code. It's always entertaining, frequently frustrating and usually enlightening. This other persons code wasn't bad; just a tad ugly. With some many people coming to Ruby, it's amazing how they tend to pervert this nice terse language into something that looks like Java. So the code snippet I worked with looked like this:

if gender == 'male'
return '1'
elsif gender == 'female'
return '2'
elsif gender.nil?
return ' '
end

So, what we have here is your basic condition block. First of all, the returns are kind of ugly. Any Ruby developer knows that you should hardly ever have to write returns. Okay, I can get rid of the returns; and, maybe I could use a case statement instead of the same. But it's about the same amount of code.

So I came up with an interesting alternative ... it uses a hash to perform the value translation, and relies on the fact that calling to_s on nil returns an empty string. Here's my one-line solution:

{'male' => '1', 'female' => '2', '' => ' '}[gender.to_s]

Got a comment about this? Let me know ...

Wednesday, October 3, 2007

My Favorite TextMate Ruby Hotkeys

I love TextMate. Though some folks may claim that any self-respecting geek should be using Emacs, I find that TextMate often surprises me in ways that make my editing experience more enjoyable and productive. Here's a review of my current (that is, till I find more) favorite hot-keys when writing Ruby (and the first two are good in any language). They are easy to remember -- they all start with Ctrl-Shift ...

Ctrl-Shift-V (Validate Syntax)
This key combination will let you know if your code is syntactically valid. You can check the syntax for most non-compiled languages that TextMate supports including Ruby, Perl, Python, PHP, HTML, and CSS. For developers used to IDEs like Eclipse that constantly provide feedback when you make a mistake, this tool is a godsend. Before I run my unit tests, I run Validate Syntax and save myself more time by catching problems, like missing end statements , earlier. When your syntax is valid it tells you everything is Okay -- when something is wrong it takes you right to the source of the error (as best as it can determine).

Ctrl-Shift-T (TODO List)
This key combination generates a todo list based on comments
in your code. When you click this key combo, TextMate scans all the files in your current project for comments indicating things to be done (TODO), to be fixed (FIXME), or to be changed (CHANGE). All you have to do is preface your comment with TODO, FIXME, or CHANGE and the TODO list can find it. It works for comments in Ruby, HTML, JavaScript and a host of other languages as well. Here's a sample using an apropos holiday theme ...

You can even customize what comment tokens are searched. Use the TODO bundle preferences if you want deviate from the defaults.

I discovered my final favorite key combo quite by accident. I was reaching for Command-Shift-R, which of course as any self-respecting Rubyist knows will run the current Ruby class in the Ruby interpreter (extremely useful for unit tests), when I accidentally hit ...

Ctrl-Shift-R (RakeMate)
This key combo searches for a Rakefile within the current project and let's you choose a Rake task to run. In this case, I ran the stats task to check on my LOCs PDQ.










The results of the task are then displayed in a separate window.



All in all, TextMate is a very capable, and lightweight IDE (yes, I said the "I" word) for Ruby.


**** WARNING: OBLIGATORY FANBOY COMMENT ****

Take a look at the Ruby and other bundles included with TextMate and you are sure to find shortcuts, and key combos that will make coding Ruby even more fun than it already is.

Monday, October 1, 2007

Where is Leopard?

It's October 1st ... Do you know where your Leopard is?

Saturday, September 29, 2007

Late nights with HTTPerf

I have just been trying out HTTPerf against my Rails app, and, for some reason the reply rate I am getting is way low -- only around 10 replies/second.
I also wonder if there is a way to test pages that require the user to login first. My whole application centers around an authenticated user ... well, 2 am and time to go to bed.