RubyMine Refactorings: Overview & Improvements

RubyMine provides a number of refactorings that can help clean up your code quickly. In mere seconds, you can move files, extract code chunks into methods and variables, and rename values across your project. These options are available through the Refactor this popup, which is accessible by selecting Refactor | Refactor this or pressing Ctrl+T / Ctrl+Alt+Shift+T.

Extract Method refactoring

To recall how refactorings work in RubyMine, let’s extract a piece of code which is repeatedly used by a number of methods, to its own method:

After creating the method and replacing the previously used code chunk with the method call, RubyMine will find all the other usages of that code chunk and suggest replacing them with the method call as well. You can review each of the occurrences, or apply the change for all of them at once:

Rename refactoring

Done! Now let’s take a look at the definition of the new method. Its name starts with the get_ prefix, which is not in line with the general style guide. So let’s use the Rename refactoring to rename it at the definition and all the method calls (Shift+F6):

As you can see, before renaming the method RubyMine finds all of its usages, so that you can review every method call that is about to be renamed.

There’s still more we could do here (e.g., turn take_user into a before_filter), but let’s move on to some new refactoring options that have just recently been improved in v2018.3 EAP.

Improved Extract Parameter refactoring

RubyMine 2018.3 EAP features an updated Extract Parameter dialog. Now, when extracting an argument you may also specify its type, provide a default value, make the argument optional, or pass it as a block.

For example, here’s a method named authenticated? which takes one parameter:

We can find all methods that use the method authenticated? with Show usages (Cmd+Alt+F7/Ctrl+Alt+F7):

Let’s now extract the remember substring from the string remember_digest and pass it as a second parameter to the method authenticated?. We’ll also use this substring as a default parameter for those methods that use authenticated?, but don’t pass their own digest. To do this, we select the desired substring and invoke the Extract Parameter option:

Now we can pass an alternative _digest attribute as a second parameter to every method that uses authenticated?:

Note that if we hadn’t provided a default parameter, RubyMine would also add the extracted parameter to all the method calls. To control this process, click the Preview button before the extraction. This will allow you to review all the methods that will get the new parameter “template”, and exclude the extraction where needed:

Extract variables and constants from substrings

Starting with this new EAP, you can move substrings to variables and constants. Like before, choose this option from the Refactor this popup, or invoke it directly with its own shortcut (Alt+Cmd+V / Ctrl+Alt+V):

Other improvements

RubyMine 2018.3 EAP also fixes a number of issues related to refactorings:

Download RubyMine 2018.3 EAP to check out its updated refactoring capabilities.

The next few Early Access builds will provide even more refactoring improvements. Meanwhile, please let us know about any issues you encounter.

Cheers,
Your RubyMine Team

This entry was posted in Feature, Productivity and tagged , . Bookmark the permalink.

One Response to RubyMine Refactorings: Overview & Improvements

  1. Pingback: RubyMine 2018.3 Goes Beta | RubyMine Blog

Leave a Reply

Your email address will not be published. Required fields are marked *