Easy Ruby development, the Eclipse way

RDT is now Aptana RadRails, but still offers Ruby developers a powerful IDE

The name of the Ruby Development Tools (RDT) plug-in and where to find the code have changed, but the basic function and licensing terms have remained essentially unchanged. If you are new to the idea of developing Ruby applications on Eclipse, start with Neal Ford's article titled "Using the Ruby Development Tools plug-in for Eclipse" to learn how to create Ruby files, how to customize the Ruby editor, how to set up the Debugger and run Ruby from Eclipse, as well as initiate tests from Eclipse. In this article, we expand on Ford's article and look at:

Code folding — Folding can be enabled for classes and methods.

— Folding can be enabled for classes and methods. Outline view — A more-detailed look at the outline view and how you can use it.

— A more-detailed look at the outline view and how you can use it. RI view — Use Ruby's Interactive documentation utility from an RDT view.

— Use Ruby's Interactive documentation utility from an RDT view. Task tags — Create tasks for configurable keywords (like TODO, FIXME) in Ruby comments.

— Create tasks for configurable keywords (like TODO, FIXME) in Ruby comments. Ruby browser — A new feature in the RDT that allows you to easily see all Ruby resources available in a project and browse them.

— A new feature in the RDT that allows you to easily see all Ruby resources available in a project and browse them. Editor improvements— Auto-complete of brackets, parentheses, and single/double quotes; and better code-assist.

Getting started

RDT, now called now called Aptana RadRails V1.0.3, is available at Aptana.com. To download a new version or update your Eclipse plug-in, follow Aptana's RadRails instructions, which will be familiar to Eclipse users. Note that if you have not installed an Aptana plug-in into your Eclipse workspace before, you should follow Aptana's instructions for Plugging Aptana into an existing Eclipse configuration. Also consult Aptana's RadRails documentation during installation to see resolutions to OS-specific issues.

Code folding

Almost all Integrated Development Environments (IDEs), such as Eclipse, support the concept of code folding. Code folding allows you to collapse sections of a source-code file while working in other parts of it. This allows you to manage larger amounts of code within one window without losing the trees for the forest. To turn on or off code folding in RadRails, simply make sure you're in the Ruby perspective and select Window > Preferences. The Preferences window should pop up. Scroll down the left-hand pane until you reach the Ruby plug-in options and expand it with Ruby > Editor > Folding. You should see something like Figure 1.

Figure 1. Eclipse Window Preferences allow you to customize the look, feel, and feature set of Eclipse perspectives

Be sure to check Enable folding when opening a new editor and select OK.

In "Using the Ruby Development Tools plug-in for Eclipse," we created a Ruby project. If you have not done so already, create a project. If you do not want to refer to that article, the code is shown in Listing 1. Create a Ruby file that consists of what is shown in Listing 1. We also need the Ruby class called hr.rb.

Listing 1. hr.rb

class Employee def initialize(name, salary, hire_year) @name = name @salary = salary @hire_year = hire_year end def to_s "Name is #(@name), salary is #(@salary), " + "hire year is #(@hire_year)" end def raise_salary_by(perc) @salary += (@salary * 0.10) end end class Manager < Employee def initialize(name, salary, hire_year, asst) super(name, salary, hire_year) @asst = asst end def to_s super + ",\tAssistan info: #{@asst}" end def raise_salary_by(perc) perc += 2007 - @hire_year super(perc) end end

You should see a series of minus signs like those in Figure 2 when viewing the class in Eclipse. Each of these minus signs represents a folding point. By selecting them, you can fold the code at this point. Try selecting the fold points for all the methods in the Employee class in hr.rb.

Figure 2. RDT support for code folding in Ruby

If you selected all the method fold points for the Employee class, you should see something like Figure 3.

Figure 3. Methods in the Employee class folded

As you can see, code folding allows you to manage and control your work environment, letting you focus on code you are interested in. If you go back to the Preferences screen to look at the Ruby code-folding settings, you will also notice that you have the option of having the RDT fold comments, methods, or inner classes on initial opening of files. Many people find this a way to see the basic structure of a class quickly without getting distracted by large amounts of code. Nevertheless, as you will see in the next section, there is an even easier way to get an overview of your code.

Outline view

In "Using the Ruby Development Tools plug-in for Eclipse," the outline view was mentioned as one method of navigating to elements in a Ruby source file. The outline view also allows you to quickly see a tree-like representation of your classes, inner classes, methods, variables, etc. If, for some reason, you do not see the outline view in your Ruby perspective, you can easily open it up by going to Window > Show View > Outline, which will open a dockable outline view of your code. For the hr.rb file, you should see something like Figure 4 when the Outline is expanded fully.

Figure 4. The outline view showing an expanded tree visualization of the hr.rb file

Each one of the symbols and colors of the symbols has a specific meaning that may or many not be obvious. If it is not, you can find information on this easily by going to Window > Preferences and selecting Ruby > Appearance > Members Sort Order, which provides a key to the outline view and lets you adjust and sort the order of how the outline view symbols are represented. You should see something like Figure 5.

Figure 5. Members Sort Order

The code outline view is a powerful tool for helping you understand how your code is structured. But what do you do when you forget what a function or class does? In the next section, we learn how to access Ruby documentation from Eclipse.

The Ruby Interactive view

Unless you possess an incredible memory, chances are you will forget some of the classes, methods, and modules Ruby offers. Ruby supplies documentation as well as Ruby Interactive (RI), an interactive documentation reader that lets you search and find information in the Ruby docs quickly. To use RI with Eclipse, you need to configure the RadRails plug-in to access it by going to Window > Preferences and selecting Ruby > RI/rdoc. Here you will see two fields: RDoc path and Ri path. Each expects the full path to the rdoc and ri scripts, which, if your Ruby install is on the C drive, would be something like C:\ruby\bin\rdoc and C:\ruby\bin\ri respectively, as shown below. (Mac OS X and Linux® users will need to adjust their paths accordingly.) Enter the correct paths and select OK.

Figure 6. Setting up the Ruby Interactive documentation reader

Once you have done this, you should be able to see an RI tab in the lower portion of your Eclipse Ruby perspective. If you for some reason do not, simply go to Window > Show View and select RI, then select that tab and type Array in the input field. The RI will try to find a match for what you are looking for or you can simply scroll through the available options and click on an item. In the case of Array , you should see something like Figure 7.

Figure 7. Example output of a query into RI for Array class

The RI view can be very helpful by allowing you to stay focused and within your development environment, instead of switching to a Web browser or another application to view the documentation. Sometimes though, you'll run into a problem you just cannot solve at the moment or do not have the time to deal with. Oftentimes, developers leave themselves a comment in the code, but these are easy to forget. In the next section, we look at using task tags to mark items for follow-up.

Task tags

One of the more useful views in Eclipse and the RadRails plug-in is the Tasks view. We can use the Tasks view as your own or a project-based to-do list, which allows you to tag code with items like ToDo, FixMe, Optimize, etc. You can configure your own task tags by going to Window > Preferences, then Ruby > Task > Tags. You should see something like Figure 8.

Figure 8. The Task tags area, which allows you to create new custom tags to mark up your code

You can use a tag just by adding it to your code, such as #ToDo: Implement give_bonus method .

Try adding that comment in the hr.rb file in the Employee class and save it. Now select the Task pane in the lower bottom of your Eclipse view, or if for some reason you do not see it, go to Window > Show View > Task and you should see something like Listing 2.

Listing 2. Viewing a task

Completed Priority Description Resource Path Location Creation Time TODO: Implement give_bonus method hr.rb RubyTest line 7 1173676801984

The ability to define your own task tags can very powerful in a team-development environment. Task tags allow the team to define specific types of tag-related tasks that they know will be seen by other developers, even if they do not look at the specific code that contains the task. This allows a team to communicate inside of the code itself, instead of requiring e-mail or other forms of communication that are easy to miss. In the next section, we look at another perspective that allows you to navigate a project and find information quickly.

The Ruby browser

There are a number of new features in this release of the RadRails plug-in, including the Ruby Browsing view. To get to this view, simply go to Window > Open Perspective > Ruby Browsing where you should see something like Figure 9.

The perspective contains multiple views of Ruby elements. The project shows all projects in your workspace. In the Types view, you will see all the classes and types in your Project, and in the Members view, you will see the specific elements of a classes selected from the Types view. Double-clicking on items will cause the RadRails plug-in to open the file associated with that element and focus on the element.

Figure 9. An example of the Ruby browsing view

Being able to browse through Ruby resources quickly offers you another way to increase your productivity. In the next section, we will look at what else has been added to the RadRails plug-in that will help you develop Ruby code.

What else is new?

The Aptana RadRails plug-in adds several enhancements to previous version of the plug-in, including code completion. For example type an E anywhere in your hr.rb file, then press Ctrl+Space, and the RadRails plug-in code-completion function will pop a list of classes, modules, globals, methods, and variables. The latest version of RDT also adds new items, such as more distinct highlighting of globals, as well as instance/class variables; the ability to turn on and off auto-closing of things like strings; JRuby support; SQL Editor; better debugging, brackets, and braces as well as other items. Aptana's RadRails Pro version adds even more functionality, including the new Ruby profiler support built into the IDE.

With the significant enhancements to the Aptana RadRails, it really comes into its own as a serious tool for developing Ruby applications. RadRails combined with other Eclipse plug-ins offers a world-class IDE with features comparable to almost any other commercial product.

Summary

This article outlined some of the additional functionality and improvements in Aptana RadRails. As you can see, the RadRails plug-in provides a capable IDE for the Ruby developer. Better yet, Eclipse offers not only a tool to work with Ruby easily but a tool with which to work with other languages. Eclipse provides a ubiquitous and common platform for development, which allows you to increase your efficiency by focusing on a language instead of learning a new IDE each time you change languages.

Downloadable resources

Related topics