BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage Articles NetBeans: Ruby Developer's New Best Friend

NetBeans: Ruby Developer's New Best Friend

This item in japanese

NetBeans is well-known as a popular and feature-rich Java IDE that was open-sourced by Sun Microsystems in 2000. And with a continuously growing user community, it is considered today to be one of the major IDEs in the field. In 2007, NetBeans began an expansion of its feature set to support many non-Java languages, including Ruby.

In this series of articles, you will discover various ways that NetBeans can make a Ruby developer's life easier and some of the features that are not available in other IDEs. We'll also cover the background of the Ruby tooling project and provide necessary information about how to get the bits, configure the IDE, and the possible ways of contributing to the project.

Why is Sun investing in Ruby?

It may come as a surprise that Sun Microsystems is investing so heavily in Ruby tools, after all, Ruby is a competitor to Java as a language. To understand Sun's motivation, we need to look at Java as a platform. Java is not only a programming language but also a Virtual Machine and a set of APIs. Today, the Java Virtual Machine (JVM) supports many different languages, unlike in the past. That JVM is no longer only about Java is emphasized by the fact that Sun bundles the Rhino JavaScript interpreter in JDK 6.

Sun recognized the value of dynamic languages - Java language is still the best for large scale enterprise applications, but dynamic languages such as Ruby can be very productive in specific areas, such as prototyping, web development or for gluing various applications together. In 2006, Sun hired the main developers of the JRuby project: Thomas Enebo and Charles Oliver Nutter, to further explore ways of integrating Ruby into the Java platform. (JRuby is a Ruby implementation written purely in Java.) The JRuby project reached an important milestone when version 1.0 was released in June 2007.

By using JRuby, developers gain many advantages of the Java platform, such as access to Java APIs and ease of deployment. The Java platform currently provides a large amount of libraries (most of them are open source) so Ruby developers can benefit from accessing these libraries directly from Ruby code if run on top of JRuby. JRuby can bring the Ruby language into places where Ruby could not be used before. Ever thought of running Ruby on a mainframe? JRuby can make it possible. The Java platform has many solutions for solving scalability, security, maintenance, deployment, and other issues of enterprise applications, and again Ruby developers can benefit from these thanks to JRuby.

Sun always provides tooling for its technologies via NetBeans and JRuby is no exception. However, if you are a Ruby or Rails developer and not interested in JRuby at all, you can still use NetBeans. NetBeans Ruby IDE ships with JRuby as the default runtime, but you can change JRuby to native Ruby in the Options dialog - you just need to point NetBeans to your Ruby installation. So NetBeans is interesting both for Java developers who are exploring the dynamic language area and for Ruby/Rails developers who are not interested in Java at all.

Three feature highlights of Ruby support in NetBeans

Let's look at three features that I consider extremely useful when developing with Ruby in NetBeans.

1. Code completion everywhere

Code completion is a context sensitive pop-up that provides users with a selection of the best possible methods, classes or modules. It can be invoked by pressing Ctrl-Space in the editor. Code completion is well-known and loved by Java developers - every modern Java IDE provides it. However, in Ruby creating good completion is a very difficult task because types are not declared in Ruby language. Thus the IDE resorts to heuristics to guess what the types are and provides options in code completion based on its guess.

Code completion shrinks the possible number of options as you type, and you can simply hit Enter to fill in the selected option:

You can see the module for each of the methods and what parameters are accepted by the method. Providing a fast way to fill in the method/class/module name and parameters is only one of many ways that code completion helps. Also extremely useful is the way that code completion shows documentation (RubyDoc) for each of the classes, methods, modules, keywords, etc.

Easy access to documentation is especially useful for beginners or developers who are migrating to Ruby from different languages - you can use code completion to learn the API as you develop. This is a friendlier way to learn a language and its APIs than to study all the details upfront. Plus, many developers prefer to experiment when learning and Ruby's clean language and API can make it easy and fun.

Code completion can also help when you type Require statements:

One of the useful features of code completion in NetBeans is support for regular expressions. Unless you are one of the few who can remember all syntactical options for regular expressions, you will find this very handy:

Similarly, if you can't remember the names or meanings of dollar variables, or what the escape codes that follow a percent sign are, NetBeans will help you:



I have only touched on some of the basic features, but there are many other ways that code completion can make your life easier. When using NetBeans for Ruby development you will definitely discover why Ctrl-Space is guaranteed to become your favorite shortcut!

2. Graphical Ruby debugger

Most Java developers can't imagine their lives without a good debugger. However, debugging in Ruby has been quite a challenge until recently. NetBeans provides a graphical debugger for Ruby, one that is very similar to Java's. You can add breakpoints into your code and run the application in debug mode and it will stop on the first breakpoint as expected. Then you can see useful views such as local and global variables; you can add watches; see the call stack, switch threads, etc. If you hover your mouse cursor over any element in your code, the expression's current value is evaluated and you can see the result instantly.

As you can see in the above screenshot, RHTML debugging is supported as well, and you can easily step through RHTML pages.

3. Ruby refactoring & quick fixes

Refactoring Ruby code until now has tended to painful: if you changed a method name you had to rename all its occurrences. With NetBeans, this process is automated so you can do things such as search for all usages of any method/class/variable in your code or invoke the rename refactoring to change the name of the element. Here's an example of the output of the find usages command:

The rename refactoring lets you change all the references in a different view:

In future versions of NetBeans's Ruby support, there will also be other refactoring available, including features such as extract method, extract local variable and others.

The IDE can sometimes detect variable collisions in code and in cases where the code may not do what the user intended. The IDE can provide a "quick fix", an easy way to correct the code:

In the screenshot above, the block variable name is the same as the local variable name, which will lead to a side effect in the code; the IDE gives you the option of renaming the variable to solve the problem.

Again, these are just some examples of how NetBeans makes the Ruby developer's life easier. I will focus on additional editing features in upcoming articles in this series.

Where to get NetBeans Ruby IDE?

There are several ways to get Ruby support for NetBeans. One way is to visit the NetBeans 6.0 download page and get the full installer, which includes Ruby support: http://dlc.sun.com/netbeans/download/6.0/milestones/latest.

However, if you prefer to install a minimal version of the Ruby IDE, you can get a daily build from here: http://deadlock.netbeans.org/hudson/job/ruby.

Another option for obtaining Ruby modules is to install the basic Java IDE and then go to the Plugin Manager in the IDE (Tools | Plugin Manager) and let NetBeans download the modules over the network and install them.

Currently, NetBeans's Ruby support is available as a preview. Although we know of many developers who already use it on commercial applications, there are bugs that will be fixed in the final version. If you encounter any bugs, please file them in NetBeans's Issuezilla: http://www.netbeans.org/kb/articles/issuezilla.html.

More information

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

We have also recorded several screencasts that highlight various new features: http://www.netbeans.org/download/flash/jruby_on_rails/jruby_on_rails.html and http://www.netbeans.org/download/flash/jruby_editing/jruby_editing.html.

Other demos and tutorials are available from the main Ruby documentation page.

In following articles in this series, we will discuss additional features of Ruby support and other 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

Adoption
Style

BT