0

I am trying to run Ruby code using Java. This code gives me an empty result. (result: null) Could any one please help me finding the error?

Java :

    public static void callRuby(){

      ScriptEngine jruby = new ScriptEngineManager().getEngineByName("jruby");
        //process a ruby file
        try {           
             jruby.eval(new BufferedReader(new FileReader("rubyTest.rb")));
             jruby.put("a", "2");
             jruby.put("b", "3");   
             System.out.println("result: " +jruby.get("res"));

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ScriptException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

}

rubyTest.rb

def addition(a,b)
puts a
puts b
res = a + b
end
2
  • did you try calling put() before eval() ? Commented Jan 28, 2015 at 15:20
  • I'm not sure it works this way, since res is a local variable that the ScriptEngine knows nothing about... try to return the value from the function and print what eval() returns... Commented Jan 28, 2015 at 15:32

2 Answers 2

2

Your rubyTest.rb script defines a function, addition(a, b), but where do you ever call that function?

This defines the function in the interpreter:

jruby.eval(new BufferedReader(new FileReader("rubyTest.rb")));

Then you assign top-level variables a and b, and then you ask for the value of res, but I don't see anywhere where your program calls the addition(a,b) function.

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

2 Comments

can you tell me please,how can i call the addition(a,b)?
Do you know Ruby? If not, time to learn. ruby-lang.org/en/documentation/quickstart Basically, you can either add the call at the end of your script file, or you can use jruby.eval(...) to evaluate a Ruby expression that calls the function. I haven't personally used JRuby from within a Java program, so I don't know the exact details. Maybe just as simple as jruby.eval("addition(a,b)");
0

James Answer contains already the solution.

It's just as simple as jruby.eval("addition(2,3)");

If you really need to work with variables a, b you can make them global (with all the sideeffects of global variables) $a, $b and assign the values like that

 jruby.eval("$a = 2");
 jruby.eval("$b = 3");
 System.out.println("result: " + jruby.eval("addition($a,$b)"));

The Bindings mechanism of Java Scripting seem not to work as the following code FAILS:

 Bindings bindings = jruby.createBindings();
 bindings.put("a", 2);
 bindings.put("a", 3);
 // variation A
 System.out.println("result: " + jruby.eval("addition(a,b)", bindings));

 // variation B
 jruby.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
 System.out.println("result: " + jruby.eval("addition(a,b)"));

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.