1

Could anyone say me, how should I use an existing Java object in a JRuby script?

I know how I can create a new instance of a Java class in JRuby, but in my case I have already an instance in Java and I want to check/modify some properties of it in my JRuby script.

//Java

public class MyClass 
{
private int size;

public int getSize() {
    return size;
}

public void setSize(int size) {
    this.size = size;
}
}

//...

MyClass myClass = new MyClass();
myClass.setSize(5);

//...

String script = "..????.."
ScriptingContainer rubyContainer = new ScriptingContainer();
rubyContainer.runScriptlet(script);

What should I write into the script variable to check and modify the size property like this:

if (myClass.getSize() > 3)
    myClass.setSize(10)
else
    myClass.setSize(0);

After the modification I'd like to be able to use the new value of the property in my Java code.

Thanks a lot!

2 Answers 2

1

Straight from the javadoc of ScriptingContainer

The second example shows how to share variables between Java and Ruby. In this example, a local variable "x" is shared. [...]

Example 2:

    ScriptingContainer container = new ScriptingContainer();
    container.put("x", 12345);
    container.runScriptlet("puts x.to_s(2)");
Sign up to request clarification or add additional context in comments.

4 Comments

but in this case I do it from my Java code. In my case I need to know either I can get the existing Java object using only the JRuby script. Say I have a Timer and every x seconds I do rubyContainer.runScriptlet(script); to do a new check/modification without doing something else in Java.
I don't understand the problem. Edit your question, and show the real thing that you would like to do with your Timer.
Suppose I have a variable in my Java code (int myInt = 5). I want to be able to achive this variable from JRuby. I initiate a ScriptingContainer and call its function runScriptlet(script). The question is what should I write into the script variable to get an access to myInt in order to check/modify it?
Use container.put("myClass", myClass), and inside the scriptlet, modify the myClass object as you will: myClass.size = 10 for example.
1

The problem is solved, thanks a lot for your help!

It is important to set "LocalVariableBehavior.PERSISTENT" to be able to use the object during multiple evaluations. After processing of the script the value of the object in the Java code is the same as in JRuby script.

//Java

rubyContainer = new ScriptingContainer(LocalVariableBehavior.PERSISTENT);
rubyContainer.put("myClass", myClass);

String script = 
    "if myClass.getSize() > 3 then " +
    "myClass.setSize(10) " +
    "else " +
    "myClass.setSize(0)" +
    "end";

rubyContainer.runScriptlet(script);

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.