Ruby Screenshot of the Week #15: More Hints and Quick Fixes!
By Tor Norbye on Aug 09, 2007
Ola Bini just posted a blog entry entitled "Ruby Is Hard". In the blog entry he shows a short code snippet which does not do what you might think upon reading it. This immediately reminded me of a dog-eared page in my worn-out copy of "Programming Ruby", specifically page 167, in the "But It Doesn't Work" section. The book covers the same problem and explains what the problem is and how to fix it.
I had marked the page for myself because when I first read it, I thought "that's something the IDE should be able to detect and warn you about!". So when I came across Ola's blog entry yesterday I figured I really should do something about it - and I had the perfect opportunity: A Java Posse recording session at Dick's new house last night. I've claimed before that writing quick fixes for Ruby in NetBeans is easy, and it really is - I completed this one as a background activity while having fun with the rest of the Posse. We are however past feature freeze for NetBeans 6, so when I'm adding fun features like this I put it into the "Experimental Hints" module, which is not part of NetBeans 6. It -is- however included in the continuous "Ruby IDE" builds you can get here - and you can also install it using the Plugin Manager in standard NetBeans.
So without further ado, here's what you get when you now open up Ola's example and the "Programming Ruby" example in NetBeans with the new hints:
As you can see, it's detecting the case there are local variable name assignments that match a writable attribute in the current class. If you hover over the warning it will tell you so:
If you hit Alt-Enter, you can see the possible fixes:
These should be pretty self-explanatory. At first you may go "Huh?" and jumping to the relevant attribute may help you figure it out. The second fix will simply change the assignment to the recommended form, and the third fix will let you rename the local variable to avoid any confusion.
In order to avoid any false positives I ran this hint over a large code base (all the Ruby libraries and Rails libraries) and it came up with a handful of warnings. These probably aren't bugs, but it's at least potentially confusing; here's
baseData.rb from Ruby's SOAP library:
There are some other experimental hints too, in particular, a hint which detects actions in your controllers that don't have associated view files and offers to create them (using the Rails code generator), as well as various name warnings. Here's a warning which detects un-Ruby-like method names and local variable names (camelCase instead of not_camel_case) - this one from a Rails test case:
Here's the current customizer. Some of the hints are not enabled by default, so you should inspect these yourself and enable those you are interested in. One hint I definitely didn't want to enable by default is the "constant name" warning. I was under the impression that constants should use all upper-case letters separated by underscores, and this is indeed the case in lots and lots of Ruby code. But there are quite a few exceptions, where constants are just capitalized - so this seems like a warning that may need some tweaking or is an individual preference. Here's the customizer:
In other news, a lot of bug fixing is happening so stay up to date with the builds :)