7

So I came up with the general idea to write some code in JRuby, then access all the classes via Java. I have no idea if this is at all possible but I wanted to ask anyway. Lets say I have some JRuby code:

class adder
   def addme
      return 22
   end
end

If I compiled this with jrubyc is there any way I could then possibly do something like this in java:

import adder;
class anything {
    void testMethod() 
    {
       adder a = new adder();
       int x = a.addme();
    }
 }

After looking at it now it sort of makes me think that Java will have zero idea what sort of item test addme is going to return so that might not work. I don't know but I wanted to throw it out there anyway.

Thanks

3 Answers 3

9

There's actually a new way to do this in JRuby 1.5! Your question is very timely. Here's an example session:

http://gist.github.com/390342

We will hopefully have a blog post detailing this new feature very soon. There's some preliminary docs on the JRuby wiki here:

http://wiki.jruby.org/GeneratingJavaClasses

1

Actually there are two ways you can call ruby code from java the first is slower but you can change at run time is to invoke the script engine like from this link. but As to how you did it, jrubyc compiles ruby to javaBytecode which means java will see it as java code

jrubyc adder.rb --java Compiling file "adder.rb" as class "Adder.class"

and just as you've done...

so you would call it like any other java class

import org.jruby.RubyObject

Adder ad = new Adder();

RubyObject ro = ad.addme();

resource

1
  • Unfortunately that didn't work for me. It couldn't find the addme() method. It's definitely there and it's public so I don't know why May 4, 2010 at 23:34
0

It's possible with the embed package in JRuby, but I think how is beyond the scope of an answer here. Check this out: http://kenai.com/projects/jruby/pages/RedBridge

1
  • That isn't natively calling the methods though, that is using a container. May 4, 2010 at 22:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.