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 ⌃T / Ctrl+Alt+Shift+T.
- Extract Method refactoring
- Rename refactoring
- Improved Extract Parameter refactoring
- Extract variables and constants from substrings
- Other improvements
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:
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):
RubyMine 2018.3 EAP also fixes a number of issues related to refactorings:
- Fixed extracting symbols to constants [RUBY-22304]
- Improved inlining variables [RUBY-22403]
- Prohibited extracting meaningless selections [RUBY-22124]
- Fixed minor renaming issues [RUBY-18812] [RUBY-18812] [RUBY-21365]
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.
Your RubyMine Team
Subscribe to Blog updates
Thanks, we've got you!
Collaborative Programming in RubyMine
Hi everyone, We're back with a new how2pro blog post unveiling another cool RubyMine feature – the Code With Me plugin. When you're knee-deep in a complex task or tangling with an issue that's testing your limits, having someone by your side can make all the difference. Additionally, in an educati…
Working With RBS in RubyMine
Starting with v2021.2, RubyMine provides code assistance for working with RBS files, along with all of the essential features that make RBS code easier to read. Here’s a brief overview. Creating RBS code Code completion Formatting and readability Navigation Refactoring Inspections Do y…
RBS: How to Get the Most Out of RubyMine’s Code Assistance
The following article shows the benefits of investing some of your time into adding RBS declarations to your project. Catch type errors RubyMine performs real-time type checking based on the information stored in .rbs files. It will show a type error if there’s a mismatch between the RBS and Ruby co…
RubyMine 2020.3.4 and 2021.1.1 introduce the concept of trusted projects. It is designed to reduce the risks associated with opening projects from unknown or untrusted sources. Check out this blog post to learn more. As you might already know, when you open a project, RubyMine runs various tools lik…