devxlogo

Ruby Developers Get Productive with NetBeans’ Ruby Pack

Ruby Developers Get Productive with NetBeans’ Ruby Pack

any developers have started to warm to the power and simplicity of Ruby. While the language is simple and powerful, one of the hits against it has been the lack of proper tool support (read this article for more information).

Increasingly a fan of NetBeans in Java development, I was recently alerted to NetBeans’ plan to provide support for Ruby (and Ruby on Rails). I have found NetBeans to be my IDE of choice in the Java arena because it is free and it supports many development project needs. From Web service, to Web application, to mobile phone application, NetBeans supports all types of Java application development and testing without the need to figure out which plug-ins are needed and how to install them. NetBeans’ convenient Update Manager makes it quick and easy to install or upgrade any application development module. This article looks at NetBeans’ Ruby Pack and how it might offer Ruby developers an important IDE option.

If you’re not familiar with Ruby, check out these Ruby tutorials I wrote for DevX this past year:

How to Get NetBeans Ruby Pack
While the NetBeans Wiki Ruby Installation page suggests four ways to get and install the Ruby Pack, the easiest way is to get a “Preview” release of NetBeans 6 with the Ruby Pack already provided. Yes, to use the Ruby Pack, you will need NetBeans 6.

NetBeans 6 is scheduled for release in November 2007 (according to the NetBeans.org news page). Development/preview downloads of version 6 can be obtained here. If you want your copy of NetBeans to come Ruby-ready, make sure you pick the option to download a “Full” NetBeans 6 copy with Ruby as well as SOA, UML, Mobility, and other pack support (166MB). Remember, early developer downloads should be used at your own risk as “dev builds are not release quality.” For completeness, it should be noted that NetBeans 6 also requires the JDK (version 5 or 6 is acceptable).

Ruby Project Creation
The NetBeans New Project wizard offers Ruby developers the ability to create a base Ruby or Ruby on Rails application. Options provide the ability to create the project from scratch or from existing source.

NetBeans has become popular for offering many types of example applications that can get people just starting out with the IDE up to speed quickly. You can load these sample applications from the New Project wizard. Unfortunately, NetBeans includes only one Ruby example—a Ruby on Rails WebLog application (see Figure 1).

When creating a standard Ruby application, the wizard just creates the project structure with a nearly empty rakefile and main Ruby code file. Rake is Ruby’s make facility (more on Rake is available here) which NetBeans can employ. In the case of a Rails application, however, the wizard creates a collection of folders, files, and standard generator scripts to support a functioning Web application. More on Rails applications is covered later.

As to be expected, NetBeans recognizes Ruby file types (.rb, .rhtml) and provides a “Ruby” file icon and appropriate file actions to the files from right click menus. Also, double-clicking any of the Ruby files in the list of project files opens the file in a Ruby-aware editor.


Figure 1. Load Example Apps: Example applications to explore and learn from, such as the Ruby on Rails WebLog, are also loaded through NetBeans New Project Wizard.
 
Figure 2. Ruby Code Folds: As with other programming languages, Ruby editors allow code blocks like modules, classes and methods to be folded to help allow developers to shrink and hide certain elements of the file while focusing on others.

Standard Editing Capabilities Plus
NetBeans Ruby Pack offers the standard code-editing conveniences that developers have come to expect and rely on. NetBeans’ Ruby Pack includes the following standard features and work, for the most part, identically to NetBeans’ Java-editing features.

  • Keyword/code-completion: Hit Ctrl-Space when typing code (keyword, class, method, etc.) and the editor offers code completion capability. Importantly, code completion/suggestion also works for regular expressions.
  • Parameter Hints: Hit Alt-P when calling a method and the editor offers parameter options.
  • Code Fold: Modules, classes, and methods can be collapsed into margin icons and {…} notation in the code (see Figure 2).
  • Background Syntax Checking: Background processes parse the code and highlight syntax errors.
  • Syntax Highlighting: Keywords, variables, classes, unused references, etc. are all color/font coded to highlight various elements in the code files.
  • Occurrences Highlighting: Placing the cursor on any reference (variable, method, or class) causes other references/declarations of the reference to be highlighted (see Figure 3).

  • Figure 3. Occurrence Highlighting: In this example, all occurrences of the height attributes are highlighted when the height attribute is selected.
     
    Figure 4. Navigator Window: This allows you to see the makeup of a Ruby file and quickly navigate to a particular method, declaration, etc.

  • Smart Selection: By pressing Alt-Shift-Up Arrow or Alt-Shift-Down Arrow, the editor will select larger or smaller code blocks around the cursor.
  • Jump to Source: Either hit alt-g or right click and select Go to —>Declaration on any class, method, variable, etc. reference and jump to its declaration.
  • Smart Indenting and Reformatting: End-statements are automatically added and the next line of code is indented properly after you hit enter when adding new class, method, or other block code (if, case, etc.). What makes this a little more interesting in Ruby is that the application of ( ) and { } are looser, but the editor manages these differences just fine. Reformatting the entire code file (Ctrl-Shift-F in a file) follows the same indentation rules.
  • Ruby Identifier Selection: The editor is aware of special Ruby notation, so double clicking to select a symbol will include identifier characters like @, $, etc. as part of the selection where appropriate.
  • Pair Matching: Along the same lines as completing a class, method, etc. definition with an end statement, the editor will detect and add a matching closing parentheses, brace, bracket, string delimiter, regular expression delimiter, etc. Whenever the cursor rests behind one of these characters it highlights the matching delimiter.
  • Navigator Window: The Navigator window displays the modules, classes, methods, attributes, etc. of the Ruby file (see Figure 4). Double clicking on any element in the Navigator takes you to the element in the Rudy source code. Filter buttons on the bottom of the navigator filter elements just as in Java editing.
  • Support for RDoc: The editor properly shows and highlights RDoc comment tags/directives. Formatting tags can also be used in the code completion documentation help (see Figure 5).

  • Figure 5. RDOC Comments: RDoc comments added to source code.
     
    Figure 6. Ruby Templates: Currently only a few templates exist for NetBeans. NetBeans creators are asking for developers to contribute templates on the Ruby Pack Wiki.

  • Code Templates: A few Ruby code templates are available for some basic tasks (see Figure 6). For example, a template exists for creating a new Ruby class. As the Wiki site suggests, more templates are needed. Perhaps Sun will make more templates available before the final release.
  • Instant Rename: Hitting Ctrl-R or right clicking and selecting Instant Rename on any variable/attribute name allows you to rename the variable and all references.

Application Execution
Both standard Ruby applications and Rails applications can be executed and tested from the IDE. Breakpoints can be set on the code and applications can also be executed in debug mode to step through the code and watch variables (see Figure 7).


Figure 7. Breakpoints and Debugging: Breakpoints are added by clicking on the far left gray bar of Ruby code editors. Code execution is stopped at the breakpoints during debugging, which allows you to step through the code and explore the variables/code stack.
 
Figure 8. Interactive Ruby Shell: This window allows you to enter and test code in an interactive mode.

Ruby’s interactive development nature is, however, kept alive through the addition of an Interactive Ruby Shell (IRB) window. The IRB window is available from NetBeans Window—>Other menu options. The IRB window allows you to write and test code dynamically (see Figure 8).

Rails applications run on a built-in WEBrick server. Requests to run the application will display on the browser that hits the WEBrick server. Again, more on NetBeans and Rails is offered below.

Gems Support
If you use a popular packaged Ruby library, NetBeans Ruby Pack offers a Gems tool to quickly and easily get, install, and update Ruby Gems. You use Gems to download and install Ruby libraries easily without having to worry about how to get a library, the latest version is, or what the libraries’ dependencies are (more information on Gems is available here). From the Tools menu bar on NetBeans, select the Ruby Gems option. The ensuing window displays the currently installed set of Ruby Gems (see Figure 9). You might note that NetBeans Ruby Pack already installs with both Rake and Rails Gems. If you want to add a Gem, push the Install New… button. NetBeans/Gems tool locates the available Gems and displays them in the Install New window (again, see Figure 9). Simply select the library you need and hit the Install button to let Gems and NetBeans install it and all the required dependencies.

Figure 9. Ruby Gems in NetBeans: Locate and install/update Gems modules through the Ruby Gems window in NetBeans. Ruby Gems is offered as an option on the Tools menu bar in NetBeans.

One critical note regarding Gems support in NetBeans is documented in the NetBeans Ruby Pack Wiki. According to the Wiki site, the Gems tool prevents you from being able to respond to prompts for those Ruby gems that require input during installation.

JRuby Under the Covers
It should be noted that what is really running under the covers of NetBeans Ruby Pack out-of-the-box is Java or more precisely JRuby. JRuby is a 100 percent Java-based Ruby interpreter. In other words, the Ruby language is interpreted and run on a JVM.

In September 2006, Sun hired the two key developers behind JRuby: Thomas Enebo and Charles Nutter. According to several statements made by Sun representatives over the past year, Sun intends to make the Java Virtual Machine a suitable platform for other programming languages to compete with the likes of Microsoft’s .NET. Running JRuby allows Ruby developers to tie into and use an extensive set of Java libraries and classes.

NetBeans allows you to change the underlying engine. If you wish to run native Ruby (written in C) use the Tools—>Options dialog window to point to the native Ruby interpreter. In the Options window, click on the Miscellaneous category and locate the Ruby Installation option. Change the location of Ruby binary (see Figure 10) to point to your native Ruby installation. Of course, this requires that you have already downloaded and installed the native Ruby environment (available here). Better support for switching to/from JRuby/Native Ruby can be anticipated with future releases of NetBeans. The ToDo page on the NetBeans Ruby Pack Wiki suggests “Better support for switching Ruby platforms (JRuby, native Ruby) ala the Java Platform Manager” is at least on the drawing board.

Figure 10. Change the Ruby Interpreter: Use the Options dialog window in Ruby to switch between the native Ruby and JRuby interpreters.

Be aware of a couple of issues/notes regarding use of native Ruby versus JRuby. If you want to use Gems with native Ruby, it must be installed with Ruby. The Ruby Gems window will then show you what Gems are installed with native Ruby and not NetBeans/JRuby. Also, when running under native Ruby, the IRB window still uses JRuby.

Indeed, the native Ruby interpreter operates a bit faster and is a little less buggy because JRuby is still relatively new. However, using JRuby allows you to take advantage of Java code from Ruby. Today, using Java code classes/libraries in your Ruby code requires some effort in that NetBeans must be informed of where to locate and load your Java code before NetBeans is started. The Ruby Pack Wiki suggests that any Java classes or library .jar files that are to be used in the Ruby code must be made available by setting up a JRUBY_EXTRA_CLASSPATH environment variable. This did not work for me. The only way I was able to get my Ruby files to see and use Java classes through JRuby was to add the classes or .jar files directly to the JRuby library folder in NetBeans. Specifically, I had to make my com.intertech.shapes.Ellipse class shown in the Ruby example code below available through a JAR that was put into the NetBeans JRuby installation folder (NetBeans 6.0 M9
uby1jruby-0.9.8lib
folder).

package com.intertech.shapes;public class Ellipse { private int major_axis, minor_axis; public Ellipse(int major, int minor) { major_axis=major; minor_axis=minor; }  public double area (){ return java.lang.Math.PI * (major_axis/2) * (minor_axis/2); }  public String toString(){ return "This ellipse has a major axis of: " + major_axis + " and a minor axis of: " + minor_axis +"
"; }}

An example Java class

require "java"require "rectangle.rb"require "square.rb"require "circle.rb"MyEllipse=Java::com.intertech.shapes.Ellipsee = MyEllipse.new(3,4)print("The area of the ellipse is: " + e.area().to_s + "
")print e.toString()r1 = Rectangle.new(3,4)print("Rectangle 1's area is: " + r1.area().to_s + "
");print("Rectangle 1's circumference is: " + r1.circumference().to_s + "

");r2 = Rectangle.new(5,7)print("Rectangle 2's area is: " + r2.area().to_s + "
");print("Rectangle 2's circumference is: " + r2.circumference().to_s + "

");Ecllipse Java class used by Ruby code.

Admittedly, it’s an ugly setup process, but again according to the Wiki site, in the future it should be easier to use and reference Java code in your Ruby applications through project references/settings. The FAQ page from the Wiki indicates: “This will not be necessary in the future; you will be able to add libraries to your Ruby projects from within the Project Properties dialog.” Remember, this is a feature only available through JRuby and not native Ruby.

Built-in Unit Testing
Also built into NetBeans Ruby Pack is the Ruby unit test facility called Test::Unit. From the File—>New File…menu window, you can request to create either a new Ruby Unit Test Suite or Ruby Unit Test.

A Test Suite is used to kick off the test cases. Dummy test case script calls are placed in the test suite Ruby script created by NetBeans. These get replaced with scripts you create. Given the code assistance that Ruby provides, creating a test case is a breeze because a simple control-space in the test case file gives you access to all the various test case methods to call upon (see Figure 11).


Figure 11. Code Assist for Test Cases: This can help you quickly pick the applicable assert method tests.
 
Figure 12. Test Suite Results: Test Cases or Test Suites are executed by running the file in NetBeans. Results are displayed in the NetBeans Output window.

To execute either a test case or a test suite, simply run the test case file or suite (Ctrl-F6). The results are displayed in the NetBeans Output window (see Figure 12).

Rails Support
Ruby on Rails is perhaps one of the key reasons Ruby has garnered so much attention. Due to metaprogramming generators (scripts) that build a lot of the Web site for you, most of the work in Rails revolves around executing appropriate scripts with the appropriate parameters.

When you create a new Ruby on Rails project in NetBeans, the metaprogramming generator scripts are created for you and added to the NetBeans project. Along with the necessary Rails scripts, NetBeans also create an instance of the built-in WEBrick Web server to run and test your application. Creating a Ruby on Rails application in NetBeans means using the IDE to call on the generator scripts and then adding the small amounts of glue code to make it all come together.

In order to demonstrate just how easy it is to build a Rails application using NetBeans, here are the steps to develop a small customer contact tool. If you are not familiar with standards Rails development, take a look at a past tutorial on Ruby on Rails here.

Figure 13. Rails Generator Scripts: The many Ruby on Rails metaprogramming generator scripts that are instrumental to Rails development, are created during Rails project creation.

First, create a Ruby on Rails application using the File—>New Project… menu option on the IDE. In the New Project window, simply select a Ruby on Rails Application and provide a project name—in this case the project is called CustomerManagement. When the finish button is pushed, NetBeans creates a project that contains all the standard Rails folders: an app folder with, controllers, helpers, models and views subfolders, a config folder, a script folder, etc. Importantly, please take a look in the script folder and note all the important rails scripts have been created (see Figure 13).

At the same time that the project and all the associated code were generated, NetBeans also set up a WEBrick server capable of running the application. In fact, right clicking on the project and selecting the Run Project menu option results in NetBeans starting the WEBrick server and launching a browser open to display the Ruby on Rails default Welcome page.

The next step is to open the database.yaml configuration file found in the Configuration folder of the project and point the application to an appropriate database. By default, Rails provides a configuration file to use a MySQL database. For this example, a small database schema called cms_development was created in MySQL. A single table was created in the schema to hold customer contact information. The SQL for that table is below.

DROP TABLE IF EXISTS `cms_development`.`contacts`;CREATE TABLE `cms_development`.`contacts` ( `id` int(10) unsigned NOT NULL auto_increment, `firstname` varchar(45) default NULL, `lastname` varchar(45) default NULL, `phone` varchar(45) default NULL, `email` varchar(45) default NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Therefore, change the database.yaml development section as shown below to give the Rails application access to the database.

development: adapter: mysql database: cms_development username: root password: root host: localhost

Next, the Ruby on Rails model must be generated using one of the scripts provided by NetBeans. Right click on the project’s Models folder and select Generate…. In the resulting Rails Generator window, enter the name Contact in the argument field to provide the name of the model to be generated. This causes Rails to create a new Ruby file in the Models folder, called contact.rb.

Figure 14. Generating a Ruby on Rails Controller: Use the NetBeans Ruby on Rails Generator script associated to the Controllers folder to create an application controller for, in this example, the contact application.

Now right click on the Controllers folder and select Generate… again. In the resulting Generator window, enter the name Contact again to name the application controller (see Figure 14). Leave the Views field empty.

Completing the Generator window results in the creation of a contact_controller.rb file in the Controllers folder. The file should also open in an code editor. Add one line code as shown below to add Rails scaffolding to the controller.

class ContactController 

And you are done! Currently, NetBeans does not have the capability to restart the WEBrick server. Again, according to the Ruby Pack Wiki site, this is a feature that will also be added in the near future. So for now, you must stop the WEBrick server and then run the project again to restart the WEBrick and the project. Once the project is running, return to the browser window where the default Welcome page is displayed. Enter the following url: http://localhost:3000/contact/list.

In a matter of seconds, you have created a Web-based simple customer contact system (see Figure 15). That's the power of Ruby on Rails with NetBeans.

Figure 15. Ruby on Rails Contact Management System via NetBeans: The results of creating a simple contact management system in only a couple of lines of code via Ruby on Rails and NetBeans.

Almost Ready for Prime Time
NetBeans has always been a heavy and CPU-intensive tool. Minimum requirements per the NetBeans release notes page are a 500 MHz Intel Pentium III or equivalent, 512 MB of memory and 850 MB of disk space. Recommended requirements are 1.4 GHz Intel Pentium III or equivalent, 1 GB of memory and free disk space. My experience has taught me that you want to be at or better than the recommended hardware in order to reap any benefits in productivity gains that NetBeans offers—including, if not more so, NetBeans Ruby Pack.

However, overall, NetBeans is a strong IDE and that strength is now available to the Ruby community or Java developers who want to start to tap into Ruby development simplicity through JRuby. While the tool is still in development, one can expect that certain features and capabilities may still have some "quirks." Most of those should be worked out before the final release of NetBeans 6. The strength of such an IDE should help quell arguments that Ruby is not ready for industrial use because it lacks proper tool/IDE support.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist