Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Ruby Developers Get Productive with NetBeans' Ruby Pack : Page 3

The simplicity and power of Ruby and Ruby on Rails have become renowned. However, finding a suitable IDE has sometimes left them wanting. With NetBeans, Ruby developers may find the productivity gains of their language are finally matched by an IDE.


advertisement
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\ruby1\jruby-0.9.8\lib 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 +"\n"; } }

An example Java class

require "java" require "rectangle.rb" require "square.rb" require "circle.rb" MyEllipse=Java::com.intertech.shapes.Ellipse e = MyEllipse.new(3,4) print("The area of the ellipse is: " + e.area().to_s + "\n") print e.toString() r1 = Rectangle.new(3,4) print("Rectangle 1's area is: " + r1.area().to_s + "\n"); print("Rectangle 1's circumference is: " + r1.circumference().to_s + "\n\n"); r2 = Rectangle.new(5,7) print("Rectangle 2's area is: " + r2.area().to_s + "\n"); print("Rectangle 2's circumference is: " + r2.circumference().to_s + "\n\n"); 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.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap