I was planning on this issue to be on Camping, but I realized that it would be good to show how to use IRB with JRuby to make life as a Java developer easier. Since Java is a compiled language, it's exceedingly hard to just test things out. My hard drive is completely littered with 5-line programs testing one aspect of the Java libraries or whatnot. If I could have an easy way to interface with the libraries without having to go through a compile cycle, this would ease my life very much. And actually, the last months I have been using JIRB in this way. Of course there is BeanShell and Groovy, but I feel they get in the way. And I'm very fond of IRB, of course.
IRB stands for Interactive Ruby, and JIRB is the name of the program for starting IRB with JRuby. Starting it should be as easy as:
and after two or three seconds you will get the Ruby prompt:
At this point, the first thing to do is to require the java libraries, so we can have something to work with:
I would first like to show how I would go about finding the behaviour of the caret in a Java regexp. (I know I could find it in the API, but this is just to show how you could do it.)
First of all, I will include the relevant class and see what I can do:
Ok, first I include the class, then I try it out. Whoopsie, there was no empty constructor for Pattern. I'm don't really remember what methods are in there, though, so I check out which methods Pattern have. There is a good many of them (and I haven't written them all out either), so I try again by grepping for a method name I believe is in there. Once I've found the method I'm looking for, I call it with the pattern I want to compile, and inspect the result in various ways. I vaguely remember a matcher method, and it seems to work as I remember:
Ok, now I have something to work with. Good, and the standard output from JRuby gives me some insight into the internals of the matcher too; interesting stuff. There is a find method, and a matches method on the Matcher:
I also belived there was a search method, and also a way to get back to the initial state of the Matcher. Hmm. Time to check out the methods of this object too:
I found the method I was looking for; reset, calls it and tries find again, and then gets the group with index 0, which is the complete match.
JIRB works great for trying out Swing interactively. I'm just going to show a tidbit of this, and since this is one of the more common examples of JRuby, you will be able to find many different demonstrations of this on the net.
We first require java-support, and including the required classes for this example:
Then we create a frame and some panels:
After this, we add some labels and some buttons to the panes, and then add the panes to the base pane, and the base pane to the frame.
Then it's just to pack the contents and show the frame:
To continue playing the Swing, just poke away at the methods. You can always change it if something goes wrong. After you're finished you can dump most of the structure very easy. Just remember to call show again to see some of the updates. Certain changes call redraw directly, but mostly it's always a good choice to call show after updates on the structures.