RubyMine: Code Insight for Ruby and Rails
This article was sponsored by JetBrains. Thank you for supporting the partners who make SitePoint possible.
If you are developing in Ruby or Rails, you may have heard of RubyMine.
RubyMine is an IDE for Ruby and Rails developed by JetBrains. It’s been around for almost 10 years and has a huge user base. And yet, as we visit industry events and shows, we often get the question: “Okay, what does RubyMine do that a text editor can’t?”. By no means am I trying to diminish the value or usefulness of free code editors, but “let’s just say, there’s a lot!” is my usual response as I fire the IDE for a quick demo. In this post I’d like to tell you about the key thing that makes RubyMine stand out: Code Insight.
Code Insight includes things like smart code completion, code navigation, language-specific inspections with quick-fixes, smart notifications, and in-editor quick documentation. So many terms, but what do they do and why would you need them? Let me have your attention for five minutes before you switch back to your $EDITOR$ and get on with your work.
Automatic Code Completion
This feature doesn’t need any introduction as it exists in all the popular editors. However, RubyMine’s autocompletion is not limited to Ruby/Rails built-in keywords and text-based autocompletion. As the IDE indexes your whole project on each startup, it can autocomplete almost any relevant entity regardless of where it’s defined. You don’t need to open a file in a separate tab to autocomplete any declarations from it – RubyMine knows everything about your project already!
Say, you are creating a new table. RubyMine will suggest possible autocompletion options of relevant column types and properties as you’d expect:
But wouldn’t you like to have access to the columns later, when you are working on a model? Of course you would, and the IDE will do that for you the same way as it autocompletes all the built-in Rails stuff:
How about the ability to also autocomplete methods, earlier defined in the model, say, in controllers or other ruby files in your project? You got it:
The IDE won’t make autocomplete suggestions for column names where you are expecting to see methods defined in the given model, and vice versa. This is a very important feature that makes the RubyMine completion stand out from other editors.
You can work with your views in the same manner. I am not talking about just the basic HTML and ERB syntax suggestions, they are a given, but actually all the specified entities in your project:
Go to declaration (aka Go to definition)
When I talk with our customers, I always ask them what their favorite RubyMine feature is. The top reply of all time is “Go to declaration rocks!”. While I’d rather hear success stories about our GUI debugger or other advanced features, positive feedback is always good! But I got curious as to why this happened time and time again, so eventually I started comparing RubyMine’s Go to declaration with Go to definition that exists in some popular editors.
Without a doubt, the IDE proved to be far more reliable when navigating to declarations. Just use ⌘+Click | Ctrl+Click (or ⌘+B | Ctrl + B if you put your caret on the desired object) and jump to the definition you need:
Not only can you easily jump to a class, method, or any other entity declaration in your project, but you can also navigate to gems and their entities’ definitions. Need to rewrite a bit of Ruby on Rails to make your project better? Say no more:
I tried to find some plugins that could do the same for the code editors I’ve been checking out lately, but they haven’t helped much. If you know some, your suggestions in the comments would be much appreciated. But until then, I’m sticking with my story: Go to declaration alone should make you want to give RubyMine a shot!
Static Analysis
What I love about Atom is its solid syntax highlighting. It won’t let me miss a closing tag or end
. I also discovered a great plugin that underlines all RuboCop offenses and even allows you to autocorrect offenses for the file. Kudos, Atom and the plugin creator, this is cool!
Until recently, I’d say this kind of RuboCop support is as good as the one we have in RubyMine. But with the latest release of RubyMine 2017.3, you are now not only able to fix the whole file with RuboCop, but even choose whether you want to autocorrect all possible offenses, a specific cop department, or even a particular offense type. You get all of this out of the box, right from the editor, and just by pressing Alt+Enter:
As for syntax checking, RubyMine is surely smart enough to provide basic syntax highlighting for Ruby, Rails, JavaScript, HTML, CSS, ERB, and more. On top of that, the IDE has a number of its own helpful intentions, like the ability to convert do … end
blocks to {}
, or modify statements to trailing conditionals. Find and apply these via the same Alt+Enter:
By the way, apart from static analysis, but in terms of intention actions, RubyMine even provides a built-in RegExp checker:
As you would expect, these and other inspections and intentions can be found and easily enabled or disabled in the settings.
Yet another Code Insight goodie is Parameter name hints. As you might guess from its name, it helps you understand the name of the argument you are passing to a method:
A great alternative to param name hints is Parameter info which can be triggered by pressing ⌘+P | Ctrl+P. For example, this may be helpful if you realize that inline hints irritate you too much and you decide to turn them off, but at some point need to check out the param names you’ve declared:
Smart notifications and actions
As another trademark feature of all JetBrains IDEs, RubyMine provides smart notifications that help users never forget about routine tasks, like configuring detected data sources, or running bundle install
after adding a new gem to the Gemfile.
There are more multiple-stage smart actions that can come in handy. For example, RubyMine has a shortcut for quick navigation between a Rails entity, e.g. a controller, and its test (Shift+⌘+T | Ctrl+Shift+T). If such a test doesn’t exist, RubyMine will suggest creating it based on the test framework you have installed. But what if you don’t have any test framework yet? In this case RubyMine will add a required gem to the Gemfile, install it for you, and then navigate you to the newly created controller test:
Speaking of Rails entities, you can also navigate to related Rails symbol with ⌃⌘↑ / Ctrl+Alt+Home.
Quick Documentation
Finally, RubyMine provides in-editor quick documentation for built-in Ruby/Rails entities, as well as entities declared in the app. This means you don’t need to surf the web each time you stumble across some alias, or search your whole app for a method declaration you can’t remember. Just put the caret on a keyword you want to look up, and hit F1. The best part is that it also works from the autocompletion list:
The above-mentioned abilities cover the stack of Code Insight features in RubyMine, but there are many more features to love in the IDE: safe refactorings, a GUI-based debugger and testing suites, and a range of navigation abilities. Check out our website to learn more about them all.
If you find any of these worth trying, go ahead and download RubyMine and use it free for a month. If your whole dev team would like to try RubyMine, you can also apply for an extended 90-day trial.
Finally, don’t forget to check if you qualify for a discounted license: OSS projects and students can apply for free JetBrains licenses, and startups may get a 50% discount!
Happy developing!