1

In Ruby, if you do something like

print ["Hello", 1, 2]

You get an output like

["Hello" , 1 , 2]


Have some ArrayList object, list in Java. It contains objects of type Object.

I am passing this list to my Ruby code. When you print this, the console outputs something like

<Java::JavaUtil::ArrayList:0x5ab077a7>

Which is correct, of course.

However, I would like to get the same behaviour as above. If list contains two objects of type Object, when Ruby pints it, I want to get

[Object:0x1234567 , Object:0x21432423]

Rather than the reference of the list itself.

I suspect I should convert list to RubyArray before passing it to Ruby. But how?

1 Answer 1

2

if you look at the API of org.jruby.RubyArray you'll find a newArray factory method :

java.util.ArrayList list = new java.util.ArrayList();
org.jruby.RubyArray array = org.jruby.RubyArray.newArray(runtime, list);

of course you need to have a reference to a org.jruby.Ruby runtime which from the Java side (depending on your usage scenario) might be obtained in a few ways :

org.jruby.Ruby.getGlobalRuntime();
org.jruby.Ruby.getThreadLocalRuntime();

also note, having a runtime instance there's several "helper" factory methods that create ruby objects in Java e.g. org.jruby.Ruby.getGlobalRuntime().newArray(list)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.