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):

Image:blockvar_RubyHints.png

The quick fix shows possible fixes:

Image: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).

Image:blockvar-fixing_RubyHints.png

Rails Deprecations

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

Image:deprecated-fields_RubyHints.png

Here's another:

Image: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

Image:sameline_RubyHints.png

After applying the above fix:

Image: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

Image:shadowvar_RubyHints.png

Alt-Enter:

Image:shadowvar-fix_RubyHints.png

Convert between {}-blocks and do/end blocks

Convert between {}-blocks and do/end blocks

Image:convertblock_RubyHints.png

Applying fix shows this:

Image: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:

Image:convert-collapse_RubyHints.png

Here's a single line block:

Image: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

Image: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...

Image: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.

Image: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)

Image: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:

Image:attribute-hint1_RubyHints.png

Here's the quick fix:

Image: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:

Image:wrong-documentation_RubyHints.png

And here's the quickfix:

Image: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:

Image:parenthesize_RubyHints.png


Extract Method

More information here.

[1]

Introduce Field, Introduce Constant, Introduce Variable

[2]

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.

Image: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.

Image: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

[3]

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:

Image:experimental-hints_RubyHints.png

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo