use of org.jruby.RubyProc in project enumerable by hraberg.
the class JRubyTest method convertRubyProcToFn.
@Test
public void convertRubyProcToFn() throws ScriptException {
RubyProc proc = (RubyProc) rb.eval("lambda {|s| s.upcase}");
assertEquals("HELLO", toFn1(proc).call("hello"));
}
use of org.jruby.RubyProc in project enumerable by hraberg.
the class JavaScriptTest method interactingWithJRuby.
@Test
public void interactingWithJRuby() throws Exception {
ScriptEngine rb = JRubyTest.getJRubyEngine();
RubyProc proc = (RubyProc) rb.eval(":*.to_proc");
FunctionFn2 f = toFunction(LambdaJRuby.toFn2(proc));
js.put("f", f);
assertEquals(6.0, js.eval("f(2, 3)"));
}
use of org.jruby.RubyProc in project enumerable by hraberg.
the class JRubyTest method convertedRubyProcRaisesArgumentErrorWhenCalledWithTooManyArguments.
@Test(expected = RaiseException.class)
public void convertedRubyProcRaisesArgumentErrorWhenCalledWithTooManyArguments() throws ScriptException {
Ruby ruby = Ruby.getGlobalRuntime();
try {
RubyProc proc = toProc(Lambda.λ(s, s.toUpperCase()));
proc.call(ruby.getThreadService().getCurrentContext(), new IRubyObject[] { ruby.newString("hello"), ruby.newString("world") });
} catch (RaiseException e) {
assertEquals(ruby.getArgumentError(), e.getException().getType());
throw e;
}
}
use of org.jruby.RubyProc in project enumerable by hraberg.
the class JRubyTest method interactingWithJavaScript.
@Test
public void interactingWithJavaScript() throws Exception {
Ruby ruby = Ruby.getGlobalRuntime();
ScriptEngine js = JavaScriptTest.getJavaScriptEngine();
Function f = (Function) js.eval("var f = function(n, m) { return n * m; }; f;");
RubyProc proc = toProc(LambdaJavaScript.toFn2(f));
assertEquals(ruby.newFloat(6), proc.call(ruby.getThreadService().getCurrentContext(), new IRubyObject[] { ruby.newFixnum(2), ruby.newFixnum(3) }));
rb.put("block", proc);
assertEquals(120.0, rb.eval("[1, 2, 3, 4, 5].inject &block"));
}
use of org.jruby.RubyProc in project enumerable by hraberg.
the class JRubyTest method convertFnToRubyProcKeepsDefaultValues.
@Test
public void convertFnToRubyProcKeepsDefaultValues() throws ScriptException {
Ruby ruby = Ruby.getGlobalRuntime();
RubyProc proc = toProc(Lambda.λ(s = "world", s.toUpperCase()));
assertEquals(ruby.newString("WORLD"), proc.call(ruby.getThreadService().getCurrentContext(), new IRubyObject[] {}));
}
Aggregations