RubyHints
Contents |
Ruby Hints And Quick Fixes
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.
Standard Hints
Block variable aliases local variable - Unintentional side effect?
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
- Rename the block variable
- Rename the local variable
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):
http://wiki.netbeans.org/wiki/attach/RubyHints/blockvar_RubyHints.png
The quick fix shows possible fixes:
http://wiki.netbeans.org/wiki/attach/RubyHints/blockvar-fixes_RubyHints.png
Invoking one of the fixes initiates instant-rename on the relevant references (either the block references or the local variable references).
http://wiki.netbeans.org/wiki/attach/RubyHints/blockvar-fixing_RubyHints.png
Rails Deprecations
Identifies deprecated Rails constructs; see http://www.rubyonrails.org/deprecation
http://wiki.netbeans.org/wiki/attach/RubyHints/deprecated-fields_RubyHints.png
Here's another:
http://wiki.netbeans.org/wiki/attach/RubyHints/deprecated-methods_RubyHints.png
Code block on single line
Code blocks on a single line can optionally be reformatted to span multiple lines
Fixes:
- Reformat code block to span multiple lines
http://wiki.netbeans.org/wiki/attach/RubyHints/sameline_RubyHints.png
After applying the above fix:
http://wiki.netbeans.org/wiki/attach/RubyHints/sameline-expanded_RubyHints.png
Additional Hints
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.
Nested local variable
Detects local variable usages that are "nested" (such as in for loops) where the loop variable is being reused
Offers the following fixes:
- Rename the inner variable
- Rename the outer variable
http://wiki.netbeans.org/wiki/attach/RubyHints/shadowvar_RubyHints.png
Alt-Enter:
http://wiki.netbeans.org/wiki/attach/RubyHints/shadowvar-fix_RubyHints.png
Convert between {}-blocks and do/end blocks
Convert between {}-blocks and do/end blocks
http://wiki.netbeans.org/wiki/attach/RubyHints/convertblock_RubyHints.png
Applying fix shows this:
http://wiki.netbeans.org/wiki/attach/RubyHints/convertblock-fix_RubyHints.png
The code can also collapse multi-line blocks into a single line block, and vice versa. Here's a multi-line block:
http://wiki.netbeans.org/wiki/attach/RubyHints/convert-collapse_RubyHints.png
Here's a single line block:
http://wiki.netbeans.org/wiki/attach/RubyHints/convert-expand_RubyHints.png
Fixes:
- Convert {}-block to a do/end-block, and collapse to a single line
- Convert {}-block to a do/end-block
- Convert do/end-block to a {}-block, and collapse to a single line
- Convert do/end-block to a {}-block
- Expand single-line block to multiple lines
- Collapse multi-line block to a single line
Uppercase constant name check
Check constant names to find CamelCase names rather than the preferred CONSTANT style
http://wiki.netbeans.org/wiki/attach/RubyHints/constantnames_RubyHints.png
CamelCase name alert
Check method names to find camelCase names instead of the preferred method_name style
Fixes:
- Rename to <name converted to underline-style>
- Rename...
http://wiki.netbeans.org/wiki/attach/RubyHints/localvarname-hint_RubyHints.png
Check identifiers for unsafe characters
Only a-z, A-Z, digits and underscore are safe in identifier names. Other international character can lead to runtime errors.
http://wiki.netbeans.org/wiki/attach/RubyHints/unsafechars_RubyHints.png
Find actions without corresponding view files
Locates actions in Rails controller files that don't have a corresponding view file
Fixes:
- Create view (open generator)
http://wiki.netbeans.org/wiki/attach/RubyHints/createview_RubyHints.png
Local Attributes
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:
- Change assignment to self.<name> to use attribute
- Rename local variable to avoid confusion
- Go to the relevant attribute definition (<attribute listed here>)
The following code sample shows the problem:
http://wiki.netbeans.org/wiki/attach/RubyHints/attribute-hint1_RubyHints.png
Here's the quick fix:
http://wiki.netbeans.org/wiki/attach/RubyHints/attribute-hint3_RubyHints.png
Error Fixes
In additional to flagging possibly bad code, hints can also be keyed off specific parser errors.
Incorrect =begin/=end blocks
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:
http://wiki.netbeans.org/wiki/attach/RubyHints/wrong-documentation_RubyHints.png
And here's the quickfix:
http://wiki.netbeans.org/wiki/attach/RubyHints/move-documentation_RubyHints.png
Parenthesize Arguments
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:
http://wiki.netbeans.org/wiki/attach/RubyHints/parenthesize_RubyHints.png
Extract Method
More information here.
http://blogs.sun.com/tor/resource/extract_method3.png
Introduce Field, Introduce Constant, Introduce Variable
http://blogs.sun.com/tor/resource/introduce2.png
More information here.
Accidental Assignments
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).
Experimental Hints
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
Reverse Conditional Logic
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)
Convert To Statement Modifier
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)
RubyGem Deprecations
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.
http://wiki.netbeans.org/wiki/attach/RubyHints/requiregem2_RubyHints.png
Other Deprecations
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).
Retry Outside Rescue
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.
http://wiki.netbeans.org/wiki/attach/RubyHints/retry_RubyHints.png
Case/When Statements With Colons
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
http://blogs.sun.com/tor/resource/when2.png
Hash List Conversion
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.
Preferences
You can enable and disable hints in the options dialog:
http://wiki.netbeans.org/wiki/attach/RubyHints/experimental-hints_RubyHints.png

