The Ruby editor supports pluggable quickfixes and hints.
More information about this is available in this blog entry and this blog entry (the material will be added to the wiki soon).
Ideas for additional quick fixes can be found in RubyCodeIdeas - please contribute your ideas there or via the issue tracker.
The block variable has the same name as a local variable, so it will reuse (and modify) the local variable which is sometimes not intended
Here's the editor showing a snippet which has a code fragment containing a block variable that has the same name as an outer local variable (and executing the block will modify the local variable):
The quick fix shows possible fixes:
Invoking one of the fixes initiates instant-rename on the relevant references (either the block references or the local variable references).
Identifies deprecated Rails constructs; see http://www.rubyonrails.org/deprecation
Here's another:
Code blocks on a single line can optionally be reformatted to span multiple lines
Fixes:
After applying the above fix:
The following hints were experimental in 6.0 (and not included in the base download, but available on the Update Center). In 6.1, these hints are all included as standard hints.
Detects local variable usages that are "nested" (such as in for loops) where the loop variable is being reused
Offers the following fixes:
Alt-Enter:
Convert between {}-blocks and do/end blocks
Applying fix shows this:
The code can also collapse multi-line blocks into a single line block, and vice versa. Here's a multi-line block:
Here's a single line block:
Fixes:
Check constant names to find CamelCase names rather than the preferred CONSTANT style
Check method names to find camelCase names instead of the preferred method_name style
Fixes:
Only a-z, A-Z, digits and underscore are safe in identifier names. Other international character can lead to runtime errors.
Locates actions in Rails controller files that don't have a corresponding view file
Fixes:
Detects cases where a local variable assignment is referring to a local variable whose name is identical to an attribute on this class, which is a common source of errors.
Fixes:
The following code sample shows the problem:
Here's the quick fix:
In additional to flagging possibly bad code, hints can also be keyed off specific parser errors.
Some users filed bugs that =begin/=end wasn't working correctly in NetBeans. The problem was however that they had indented their =begin/=end block, and in Ruby, it *must* appear in column 0. Thus, this hint describes the problem and offers to automatically fix it.
Here's the error - notice the lightbulb next to the error stop sign:
And here's the quickfix:
Ruby warns when expressions with arguments really should have parentheses to avoid confusion (and the code may be disallowed in the future). NetBeans offers to fix these problems:
More information here.
More information here.
This tip detects mistakes like the following
x = 1 y = 2 puts "equal" if x = y
It also contains a quickfix to fix the problem (change it to x == y).
These hints are experimental in the sense that they were recently added. They are not part of the standard download; instead, they are included in the "Extra Hints" plugin, available from the update center. They are also included in the daily builds on deadlock.netbeans.org
This quickfix checks if the caret is inside an if or unless statement, and if so, it offers to replace statements of this form:
if !x
...
with
unless x
...
and similarly,
if foo != bar
...
with
unless == bar
...
(The opposite scenario, converting unless !x to if x is also supported).
(You can see some screenshots of this in this blog entry)
This hint will convert if/unless statements of this form:
if foo bar end
into
bar if foo
(You can see some screenshots of this in this blog entry)
RubyGems 1.0 is out and has removed the old Kernel#require_gem method (which is used by older versions of Rails for example). The deprecation quickfix identifies these usages and offers to fix them.
NetBeans also checks for many other deprecated usages. Attempting to require getopts, parsearg, printenv or cgi-lib will generate warnings, as will the assert_raises method from Test::Unit (deprecated in Rails 1.9).
It was just decided that "retry" must be inside a rescue statement in Ruby 1.9. This rule detects code where this is not the case, and warns about it such that you can update your code to work on Ruby 1.9.
As of Ruby 1.9, you cannot use colons to separate when statements. This quickfix detects this problem and offers to fix it.
More details in this blog entry
As of Ruby 1.9, you can no longer write hashes like this:
{ "a", "b", "c", "d" }
You must instead use the following form:
{ "a" => "b", "c" => "d" }
This quickfix detects usages of the former construct and offers to convert it to the latter.
You can enable and disable hints in the options dialog:
| attribute-hint1.png | ![]() |
24036 bytes |
| attribute-hint3.png | ![]() |
27524 bytes |
| blockvar-fixes.png | ![]() |
27939 bytes |
| blockvar-fixing.png | ![]() |
26149 bytes |
| blockvar.png | ![]() |
29557 bytes |
| constantnames.png | ![]() |
10519 bytes |
| convert-collapse.png | ![]() |
25308 bytes |
| convert-expand.png | ![]() |
39242 bytes |
| convertblock-fix.png | ![]() |
29132 bytes |
| convertblock.png | ![]() |
9429 bytes |
| createview.png | ![]() |
20887 bytes |
| deprecated-fields.png | ![]() |
35776 bytes |
| deprecated-methods.png | ![]() |
19081 bytes |
| experimental-hints.png | ![]() |
96116 bytes |
| localvarname-hint.png | ![]() |
38600 bytes |
| move-documentation.png | ![]() |
13825 bytes |
| parenthesize.png | ![]() |
21199 bytes |
| requiregem2.png | ![]() |
20037 bytes |
| retry.png | ![]() |
10037 bytes |
| sameline-expanded.png | ![]() |
14017 bytes |
| sameline.png | ![]() |
18429 bytes |
| shadowvar-fix.png | ![]() |
20756 bytes |
| shadowvar.png | ![]() |
9416 bytes |
| unsafechars.png | ![]() |
10835 bytes |
| wrong-documentation.png | ![]() |
9859 bytes |