19 January 2008

Using JRuby when package names include capitals

I discovered solutions to a few problems I was having using JRuby with our Ruby libraries at work. The first relates to our domain name and "magic" names in JRuby. For example, you can write something like:

org.junit.Assert.assertEquals("Silly example", 4, 4)

This code will run just fine. But now consider my company - samba.biz. From the example above, I expected to be able to write:

biz.samba.Assert.assert_equals("Silly example", 4, 4)

But this gives me "NameError: undefined local variable or method 'biz' for main:Object". It turns out that 'com' and 'org' (and perhaps others?) are "magic" names that support this syntax, but 'biz' is not. After some fooling around, I was able to get the above example to work like this:

include_class 'biz.samba.Assert'
Assert.assert_equals("Silly example", 4, 4)

This also solved another problem I was having at work. For some reason, we have some package names that include capital letters (why?). For example

biz.samba.backend.states.CA.utils.ProcessEpnsByList

JRuby wanted to think that CA was a class name, since it starts with a capital letter. Again, the syntax above solves this issue.

http://blogs.sun.com/coolstuff/entry/using_java_classes_in_jruby
was a big help in helping figure this out.