Tuesday, September 25, 2007

JRuby Enum Constants and my irrational fear of ::'s

So development on PeerTAB is rolling along. Sort of. I am continuing to play with JXTA via JRuby.

So in Java
manager = new NetworkManager(NetworkManager.ConfigMode.EDGE, "HelloWorld");

But of course in JRuby
include_class "net.jxta.platform.NetworkManager"
m = NetworkManager.new(NetworkManager.ConfigMode.EDGE, "HelloWorld")

fails with

irb(main):010:0> NetworkManager.ConfigMode
NoMethodError: undefined method `ConfigMode' for Java::NetJxtaPlatform::NetworkManager:Class
from (irb):10:in `binding'
from /home/jxta/jruby-1.0.1/lib/ruby/1.8/irb.rb:150:in `eval_input'
from /home/jxta/jruby-1.0.1/lib/ruby/1.8/irb.rb:70:in `signal_status'
from /home/jxta/jruby-1.0.1/lib/ruby/1.8/irb.rb:147:in `eval_input'
from /home/jxta/jruby-1.0.1/lib/ruby/1.8/irb.rb:70:in `each_top_level_statement'
from /home/jxta/jruby-1.0.1/lib/ruby/1.8/irb.rb:146:in `loop'
from /home/jxta/jruby-1.0.1/lib/ruby/1.8/irb.rb:146:in `catch'
from /home/jxta/jruby-1.0.1/lib/ruby/1.8/irb.rb:146:in `eval_input'
from /home/jxta/jruby-1.0.1/lib/ruby/1.8/irb.rb:70:in `start'
from :1:in `catch'
from /home/jxta/jruby-1.0.1/lib/ruby/1.8/irb.rb:69:in `start'
from :1

So the question then I how do I get these enum constants?

So interestingly enough

irb(main):020:0> NetworkManager.constants
=> ["ConfigMode"]
irb(main):021:0>


But that did get me anywhere and it was only until I ran across this blog post that I figured it out.


irb(main):011:0> NetworkManager::ConfigMode
=> Java::NetJxtaPlatform::NetworkManager::ConfigMode
irb(main):012:0> NetworkManager.constants
=> ["ConfigMode"]
irb(main):013:0> NetworkManager.constants.class
=> Array
irb(main):014:0> NetworkManager::ConfigMode::EDGE
=> #
irb(main):015:0>

And I know part of the problem is I refuse to use :: unless I'm forced to because it seems way too Perl-ish and ugly. And yeah, I've heard there is something called C++.

1 comment:

Da said...

Hi, how are you going playing with JXTA and Jruby? I am gonna be involved in a P2P project, and want to implement it on Jruby, but I'm still a little reluctant...

Good luck.