Hiya List,
In my previous e-mail I described how I want to use JRuby to learn
Java (in order to used a kewl Java-API).
Also I expressed some thoughts about how I can mentally relate the
Java package declaration to my way of doing things in Ruby.
This Java-API comes with a sample program.
In order to get-good with this API, I’ll try to “port” the sample
program to JRuby.
It is an exercise designed to flow some knowledge into my brain.
So, here is the first bit of syntax I want to port to JRuby:
/*
- Main.java
*/
package TestJavaClient;
import java.awt.Component;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
public class Main {
// This method is called to start the application
public static void main (String args[]) {
SampleFrame sampleFrame = new SampleFrame();
sampleFrame.setVisible(true);
}
static public void inform( final Component parent, final String str)
{
if( SwingUtilities.isEventDispatchThread() ) {
showMsg( parent, str, JOptionPane.INFORMATION_MESSAGE);
}
else {
SwingUtilities.invokeLater( new Runnable() {
public void run() {
showMsg( parent, str, JOptionPane.INFORMATION_MESSAGE);
}
});
}
}
static private void showMsg( Component parent, String str, int type)
{
// this function pops up a dlg box displaying a message
JOptionPane.showMessageDialog( parent, str, “IB Java Test
Client”, type);
}
}
If it looks ugly in your e-mail I put a copy in pastie.org:
So, again, my aim is to port the above Main.java file into main.rb
The first line I see is this:
package TestJavaClient;
I’m tempted to ignore it but perhaps I will eventually morph it into a
Ruby-module declaration.
The next 3 lines are easy to port:
import java.awt.Component;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
They become this:
#!/usr/bin/env jruby
main.rb
require ‘java’
include_class java.awt.Component
include_class javax.swing.JOptionPane
include_class javax.swing.SwingUtilities
Next, I am looking at this Java code:
public class Main {
// This method is called to start the application
public static void main (String args[]) {
SampleFrame sampleFrame = new SampleFrame();
sampleFrame.setVisible(true);
}
}
Perhaps a proper port of that Java code to JRuby would be this:
#!/usr/bin/env jruby
main.rb
require ‘java’
include_class java.awt.Component
include_class javax.swing.JOptionPane
include_class javax.swing.SwingUtilities
class Main
def main(args= nil)
sample_frame = SampleFrame.new
sample_frame.set_visible(true)
end
end
??
I tried running it on my Mac and it ran error free.
During the 1 second that it ran it popped up a little-white-coffee-cup
in my doc.
I think I starting to scratch the surface of this problem.
Here is the next bit of code I want to port:
static public void inform( final Component parent, final String str)
{
// java code here
}
I have no idea what the “final” declarations do in the argument list
but I am tempted to ignore them.
Perhaps this would do it:
def inform(parent, str)
# JRuby code here
end
??
Thoughts anyone?
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email