[RSS]

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.png

The quick fix shows possible fixes:

http://wiki.netbeans.org/wiki/attach/RubyHints/blockvar-fixes.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.png

Rails Deprecations

Identifies deprecated Rails constructs; see http://www.rubyonrails.org/deprecation

http://wiki.netbeans.org/wiki/attach/RubyHints/deprecated-fields.png

Here's another:

http://wiki.netbeans.org/wiki/attach/RubyHints/deprecated-methods.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.png

After applying the above fix:

http://wiki.netbeans.org/wiki/attach/RubyHints/sameline-expanded.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.png

Alt-Enter:

http://wiki.netbeans.org/wiki/attach/RubyHints/shadowvar-fix.png

Convert between {}-blocks and do/end blocks

Convert between {}-blocks and do/end blocks

http://wiki.netbeans.org/wiki/attach/RubyHints/convertblock.png

Applying fix shows this:

http://wiki.netbeans.org/wiki/attach/RubyHints/convertblock-fix.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.png

Here's a single line block:

http://wiki.netbeans.org/wiki/attach/RubyHints/convert-expand.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.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.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.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.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.png

Here's the quick fix:

http://wiki.netbeans.org/wiki/attach/RubyHints/attribute-hint3.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.png

And here's the quickfix:

http://wiki.netbeans.org/wiki/attach/RubyHints/move-documentation.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.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.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.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.png

Attachments

attribute-hint1.png Info on attribute-hint1.png 24036 bytes
attribute-hint3.png Info on attribute-hint3.png 27524 bytes
blockvar-fixes.png Info on blockvar-fixes.png 27939 bytes
blockvar-fixing.png Info on blockvar-fixing.png 26149 bytes
blockvar.png Info on blockvar.png 29557 bytes
constantnames.png Info on constantnames.png 10519 bytes
convert-collapse.png Info on convert-collapse.png 25308 bytes
convert-expand.png Info on convert-expand.png 39242 bytes
convertblock-fix.png Info on convertblock-fix.png 29132 bytes
convertblock.png Info on convertblock.png 9429 bytes
createview.png Info on createview.png 20887 bytes
deprecated-fields.png Info on deprecated-fields.png 35776 bytes
deprecated-methods.png Info on deprecated-methods.png 19081 bytes
experimental-hints.png Info on experimental-hints.png 96116 bytes
localvarname-hint.png Info on localvarname-hint.png 38600 bytes
move-documentation.png Info on move-documentation.png 13825 bytes
parenthesize.png Info on parenthesize.png 21199 bytes
requiregem2.png Info on requiregem2.png 20037 bytes
retry.png Info on retry.png 10037 bytes
sameline-expanded.png Info on sameline-expanded.png 14017 bytes
sameline.png Info on sameline.png 18429 bytes
shadowvar-fix.png Info on shadowvar-fix.png 20756 bytes
shadowvar.png Info on shadowvar.png 9416 bytes
unsafechars.png Info on unsafechars.png 10835 bytes
wrong-documentation.png Info on wrong-documentation.png 9859 bytes