Search in sources :

Example 1 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project enumerable by hraberg.

the class JRubyTest method interactingWithScala.

@SuppressWarnings("unchecked")
@Test
public void interactingWithScala() throws Exception {
    Ruby ruby = Ruby.getGlobalRuntime();
    ScalaInterpreter scala = ScalaTest.getScalaInterpreter();
    Function2<Integer, Integer, Integer> f = (Function2<Integer, Integer, Integer>) scala.eval("(n: Long, m: Long) => n * m");
    RubyProc proc = toProc(LambdaScala.toFn2(f));
    assertEquals(ruby.newFixnum(6), proc.call(ruby.getThreadService().getCurrentContext(), new IRubyObject[] { ruby.newFixnum(2), ruby.newFixnum(3) }));
    rb.put("block", proc);
    assertEquals(120L, rb.eval("[1, 2, 3, 4, 5].inject &block"));
}
Also used : RubyProc(org.jruby.RubyProc) ScalaInterpreter(org.enumerable.lambda.support.scala.ScalaTest.ScalaInterpreter) Function2(scala.Function2) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Ruby(org.jruby.Ruby) LambdaJRuby(org.enumerable.lambda.support.jruby.LambdaJRuby) GroovyTest(org.enumerable.lambda.support.groovy.GroovyTest) ScalaTest(org.enumerable.lambda.support.scala.ScalaTest) Test(org.junit.Test) ClojureTest(org.enumerable.lambda.support.clojure.ClojureTest) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

Example 2 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project enumerable by hraberg.

the class JRubyTest method convertFnToRubyProc.

@Test
public void convertFnToRubyProc() throws ScriptException {
    Ruby ruby = Ruby.getGlobalRuntime();
    RubyProc proc = toProc(Lambda.λ(s, s.toUpperCase()));
    assertEquals(ruby.newString("HELLO"), proc.call(ruby.getThreadService().getCurrentContext(), new IRubyObject[] { ruby.newString("hello") }));
}
Also used : RubyProc(org.jruby.RubyProc) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Ruby(org.jruby.Ruby) LambdaJRuby(org.enumerable.lambda.support.jruby.LambdaJRuby) GroovyTest(org.enumerable.lambda.support.groovy.GroovyTest) ScalaTest(org.enumerable.lambda.support.scala.ScalaTest) Test(org.junit.Test) ClojureTest(org.enumerable.lambda.support.clojure.ClojureTest) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

Example 3 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project enumerable by hraberg.

the class JRubyTest method interactingWithGroovy.

@Test
public void interactingWithGroovy() throws Exception {
    Ruby ruby = Ruby.getGlobalRuntime();
    ScriptEngine groovy = GroovyTest.getGroovyEngine();
    Closure<?> closure = (Closure<?>) groovy.eval("{ n, m -> n * m }");
    RubyProc proc = toProc(LambdaGroovy.toFn2(closure));
    assertEquals(ruby.newFixnum(6), proc.call(ruby.getThreadService().getCurrentContext(), new IRubyObject[] { ruby.newFixnum(2), ruby.newFixnum(3) }));
    rb.put("block", proc);
    assertEquals(120L, rb.eval("[1, 2, 3, 4, 5].inject &block"));
}
Also used : RubyProc(org.jruby.RubyProc) Closure(groovy.lang.Closure) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Ruby(org.jruby.Ruby) LambdaJRuby(org.enumerable.lambda.support.jruby.LambdaJRuby) ScriptEngine(javax.script.ScriptEngine) GroovyTest(org.enumerable.lambda.support.groovy.GroovyTest) ScalaTest(org.enumerable.lambda.support.scala.ScalaTest) Test(org.junit.Test) ClojureTest(org.enumerable.lambda.support.clojure.ClojureTest) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

Example 4 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project enumerable by hraberg.

the class JRubyTest method interactingWithClojure.

@Test
public void interactingWithClojure() throws Exception {
    Ruby ruby = Ruby.getGlobalRuntime();
    IFn star = (IFn) ClojureTest.getClojureEngine().eval("*");
    RubyProc proc = toProc(LambdaClojure.toFn2(star));
    assertEquals(ruby.newFixnum(6), proc.call(ruby.getThreadService().getCurrentContext(), new IRubyObject[] { ruby.newFixnum(2), ruby.newFixnum(3) }));
    rb.put("block", proc);
    assertEquals(120L, rb.eval("[1, 2, 3, 4, 5].inject &block"));
}
Also used : IFn(clojure.lang.IFn) RubyProc(org.jruby.RubyProc) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Ruby(org.jruby.Ruby) LambdaJRuby(org.enumerable.lambda.support.jruby.LambdaJRuby) GroovyTest(org.enumerable.lambda.support.groovy.GroovyTest) ScalaTest(org.enumerable.lambda.support.scala.ScalaTest) Test(org.junit.Test) ClojureTest(org.enumerable.lambda.support.clojure.ClojureTest) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

Example 5 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.

the class XmlReader method from_io.

@JRubyMethod(meta = true, rest = true)
public static IRubyObject from_io(ThreadContext context, IRubyObject cls, IRubyObject[] args) {
    // Only to pass the  source test.
    Ruby runtime = context.getRuntime();
    // Not nil allowed!
    if (args[0].isNil())
        throw runtime.newArgumentError("io cannot be nil");
    XmlReader reader = (XmlReader) NokogiriService.XML_READER_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Reader"));
    reader.init(runtime);
    reader.setInstanceVariable("@source", args[0]);
    reader.setInstanceVariable("@errors", runtime.newArray());
    IRubyObject url = context.nil;
    if (args.length > 1)
        url = args[1];
    if (args.length > 2)
        reader.setInstanceVariable("@encoding", args[2]);
    Options options;
    if (args.length > 3) {
        options = new ParserContext.Options((Long) args[3].toJava(Long.class));
    } else {
        // use the default options RECOVER | NONET
        options = new ParserContext.Options(2048 | 1);
    }
    InputStream in = new UncloseableInputStream(new IOInputStream(args[0]));
    reader.setInput(context, in, url, options);
    return reader;
}
Also used : Options(nokogiri.internals.ParserContext.Options) Options(nokogiri.internals.ParserContext.Options) UncloseableInputStream(nokogiri.internals.UncloseableInputStream) IOInputStream(org.jruby.util.IOInputStream) InputStream(java.io.InputStream) UncloseableInputStream(nokogiri.internals.UncloseableInputStream) IRubyObject(org.jruby.runtime.builtin.IRubyObject) ParserContext(nokogiri.internals.ParserContext) Ruby(org.jruby.Ruby) IOInputStream(org.jruby.util.IOInputStream) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

IRubyObject (org.jruby.runtime.builtin.IRubyObject)105 JRubyMethod (org.jruby.anno.JRubyMethod)32 Document (org.w3c.dom.Document)27 Ruby (org.jruby.Ruby)25 Node (org.w3c.dom.Node)24 RubyString (org.jruby.RubyString)16 RubyArray (org.jruby.RubyArray)13 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)11 RaiseException (org.jruby.exceptions.RaiseException)10 InputStream (java.io.InputStream)6 ClojureTest (org.enumerable.lambda.support.clojure.ClojureTest)6 GroovyTest (org.enumerable.lambda.support.groovy.GroovyTest)6 JavaScriptTest (org.enumerable.lambda.support.javascript.JavaScriptTest)6 LambdaJRuby (org.enumerable.lambda.support.jruby.LambdaJRuby)6 ScalaTest (org.enumerable.lambda.support.scala.ScalaTest)6 RubyProc (org.jruby.RubyProc)6 Test (org.junit.Test)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IOException (java.io.IOException)4 NokogiriHelpers.clearCachedNode (nokogiri.internals.NokogiriHelpers.clearCachedNode)4