Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage Articles NetBeans: Ruby Developer's New Best Friend (Part 2)

NetBeans: Ruby Developer's New Best Friend (Part 2)

This item in japanese

This article is a second part in series of articles about NetBeans & Ruby. You can read the first part of the article which is an introduction to the NetBeans Ruby support here.

In this part I am going to focus on Ruby on Rails tooling and various additional functionality that help you being productive inside the editor and outside of it. An IDE stands for Integrated Development Environment so you'll see that by using NetBeans you don't need to switch to the command line anymore to work with Ruby and Rails.

Ruby on Rails support

When creating a new Ruby application in NetBeans you can choose from different templates -- you can either create a regular Ruby application or a Ruby on Rails application.

When creating a Rails project the IDE lets you choose whether you want to use native Ruby or JRuby, which of the database drivers you want to use (including JDBC in case of JRuby) and you can also update to the most recent version of Rails easily. Once you confirm the New Project dialog, the project is created with default directory structure. This is a great advantage of Rails because all applications look familiar, so you can get easily get started when you come to an application that somebody else created. You get a nice logical overview of the files in NetBeans' project explorer and you can navigate to different files easily:

You can also use all Rails generators right in the IDE - when you right click on the project you can choose which of the generators to use, fill in the parameters and run it. Then you see the output of the generator in the output window.

You can leverage code completion, refactoring, quick fixes and other features in the editor while developing the application (see part one of this article to learn more about some of the editing features). When you are ready to run the application you can just hit the Run button without any necessary configuration - NetBeans bundles the Webrick server you can use to run your application. A web browser window is opened automatically and the IDE will run the Webrick server in debug mode in case you choose to debug your application. You can of course switch your server to Mongrel easily, too.

Rake is integrated into the IDE as well, so you can run Rake targets on your project:

You can also debug rake targets easily, run migration scripts and work with the Rails console right from the IDE. You can also install different Rails plugins using a special dialog. There are other features for Rails development available, but hopefully you got the idea of how you can develop Rails applications using NetBeans.

Editing Features

I already discussed some of the editing features in part one of this article (code completion and refactoring). The editor is what I think most Ruby developers will like about NetBeans - there are many different little things that help all the time. For example, when you create a new method an end statement is filled in automatically and your cursor is placed on the line in between. You can see all occurrences of different elements such as variables in the file:

When I want to rename the variable I can simply press Ctrl-R and all occurrences in the file get renamed at once:

Easy orientation in code is enabled thanks to nice syntax coloring even in RubyDocs. Errors get underlined in the editor as you type and NetBeans shows you which of the variables are not used, so you can remove them.

When you hold the CTRL key and move the mouse cursor over any class, method or variable you can go to it's declaration. You even get a small hit about what the selected element does.

You can also use code templates. Many code templates are pre-defined for you but you can also create your own templates easily. In the template you can specify position of the cursor after expansion and you can pre-fill various variables as well. So the templates are very useful when you use some API repetitively.

There are many other editing features available so check out the Ruby editing wiki page to find out more:

Gem Manager

The Gem manager (Tools | Ruby Gems) lets you install new versions of gems, browse already installed gems, update them and so on, as seen on the screenshot below. The user interface is very similar to the NetBeans Plugin manager and Rails Plugin manager, so you'll get used to it easily. There are many gems available in the Ruby repository, and so fulltext search becomes very useful when you want to get some library.

Ruby Options

As I mentioned earlier you can switch between native Ruby and JRuby. The IDE uses JRuby by default because it comes bundled but you can simply point the IDE to your existing Ruby installation directory and it will pick it up, no matter which operating system you use.

You can also switch between classic debugger and fast debugger using the Options dialog. In case of JRuby you can only use the classic debugger, which is slower but works in all the cases. With native Ruby you can use the fast debugger (you can install it easily, it gets downloaded in the IDE) which is the recommended debugger for Ruby. There are two more Option tabs available: "formatting" which lets you control formatting settings in the editor and "hints" that control appearance of the light bulb in the editor.

Unit Testing

Unit testing support is very well integrated, too. For example when you create a new model a unit test is automatically created for you. This is a feature of Ruby on Rails, the philosophy is that testing is so important that it cannot be omitted. You can run tests right from the editor and navigate to test classes, too.

If you are really into testing you can install the "ZenTest" Ruby Gem (use Tools | Ruby Gems), and then your projects will have an "AutoTest" menu item in their context menu. When you invoke it, it will launch AutoTest on your project, which will run unit tests automatically whenever you modify a file. AutoTest can in many cases figure out which unit tests need to be run - this is especially true for Rails projects. If not, it will run all unit tests.

Using the console

Even though we tried to integrate as much of functionality as possible into the IDE, you may still want to access the console. This can be useful for example when you want to try out some of the ruby APIs interactively. The Ruby shell can be opened using Window | Other | Ruby Shell (IRB). The console is opened in the bottom of the IDE and you can take advantage of the full power of Ruby's interactive language.

More information

You can find additional information about Ruby support in NetBeans on its wiki page:

Various demos and tutorials are available from the main Ruby documentation page:

Finally you can download the NetBeans IDE for free from the following URL: You can either choose to install a small version of the Ruby-only IDE (a special bundle is provided) or full-blown Java IDE with Ruby support. It is also possible to add Ruby support to the Java-only IDE using Tools | Plugins.

In the last part of the series, we will discuss extra features you can get by installing various experimental plugins and community-related topics, such as how to get involved in the project and its future plans. In the meantime, make sure to try out Ruby support in NetBeans. If you develop in Ruby a lot, you may find that NetBeans is the IDE you have been looking for!

Rate this Article