org.jruby.RubyNil Java Examples

The following examples show how to use org.jruby.RubyNil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JRubyScriptUtils.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a new JRuby-scripted object from the given script source.
 * @param scriptSource the script source text
 * @param interfaces the interfaces that the scripted Java object is to implement
 * @param classLoader the {@link ClassLoader} to create the script proxy with
 * @return the scripted Java object
 * @throws JumpException in case of JRuby parsing failure
 */
public static Object createJRubyObject(String scriptSource, Class<?>[] interfaces, ClassLoader classLoader) {
	Ruby ruby = initializeRuntime();

	Node scriptRootNode = ruby.parseEval(scriptSource, "", null, 0);
       IRubyObject rubyObject = ruby.runNormally(scriptRootNode);

	if (rubyObject instanceof RubyNil) {
		String className = findClassName(scriptRootNode);
		rubyObject = ruby.evalScriptlet("\n" + className + ".new");
	}
	// still null?
	if (rubyObject instanceof RubyNil) {
		throw new IllegalStateException("Compilation of JRuby script returned RubyNil: " + rubyObject);
	}

	return Proxy.newProxyInstance(classLoader, interfaces, new RubyObjectInvocationHandler(rubyObject, ruby));
}
 
Example #2
Source File: JRubyScriptUtils.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new JRuby-scripted object from the given script source.
 * @param scriptSource the script source text
 * @param interfaces the interfaces that the scripted Java object is to implement
 * @param classLoader the {@link ClassLoader} to create the script proxy with
 * @return the scripted Java object
 * @throws JumpException in case of JRuby parsing failure
 */
public static Object createJRubyObject(String scriptSource, Class<?>[] interfaces, ClassLoader classLoader) {
	Ruby ruby = initializeRuntime();

	Node scriptRootNode = ruby.parseEval(scriptSource, "", null, 0);
       IRubyObject rubyObject = ruby.runNormally(scriptRootNode);

	if (rubyObject instanceof RubyNil) {
		String className = findClassName(scriptRootNode);
		rubyObject = ruby.evalScriptlet("\n" + className + ".new");
	}
	// still null?
	if (rubyObject instanceof RubyNil) {
		throw new IllegalStateException("Compilation of JRuby script returned RubyNil: " + rubyObject);
	}

	return Proxy.newProxyInstance(classLoader, interfaces, new RubyObjectInvocationHandler(rubyObject, ruby));
}
 
Example #3
Source File: PigJrubyLibrary.java    From spork with Apache License 2.0 5 votes vote down vote up
/**
 * This method facilitates conversion from Ruby objects to Pig objects. This is
 * a general class which detects the subclass and invokes the appropriate conversion
 * routine. It will fail on an unsupported datatype.
 *
 * @param  rbObject      a Ruby object to convert
 * @return               the Pig analogue of the Ruby object
 * @throws ExecException if rbObject is not of a known type that can be converted
 */
@SuppressWarnings("unchecked")
public static Object rubyToPig(IRubyObject rbObject) throws ExecException {
    if (rbObject == null || rbObject instanceof RubyNil) {
        return null;
    } else if (rbObject instanceof RubyArray) {
        return rubyToPig((RubyArray)rbObject);
    } else if (rbObject instanceof RubyHash) {
        return rubyToPig((RubyHash)rbObject);
    } else if (rbObject instanceof RubyString) {
        return rubyToPig((RubyString)rbObject);
    } else if (rbObject instanceof RubyBignum) {
        return rubyToPig((RubyBignum)rbObject);
    } else if (rbObject instanceof RubyFixnum) {
        return rubyToPig((RubyFixnum)rbObject);
    } else if (rbObject instanceof RubyFloat) {
        return rubyToPig((RubyFloat)rbObject);
    } else if (rbObject instanceof RubyInteger) {
        return rubyToPig((RubyInteger)rbObject);
    } else if (rbObject instanceof RubyDataBag) {
        return rubyToPig((RubyDataBag)rbObject);
    } else if (rbObject instanceof RubyDataByteArray) {
        return rubyToPig((RubyDataByteArray)rbObject);
    } else if (rbObject instanceof RubySchema) {
        return rubyToPig((RubySchema)rbObject);
    } else if (rbObject instanceof RubyBoolean) {
        return rubyToPig((RubyBoolean)rbObject);
    } else {
        throw new ExecException("Cannot cast into any pig supported type: " + rbObject.getClass().getName());
    }
}
 
Example #4
Source File: TableImpl.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public Row get(int index) {
    IRubyObject o = rubyArray.at(rubyArray.getRuntime().newFixnum(index));
    if (o == null || o instanceof RubyNil) {
        return null;
    }
    return new RowImpl(o);
}
 
Example #5
Source File: RubyObjectWrapper.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
public String getString(String propertyName, Object... args) {
    IRubyObject result = getRubyProperty(propertyName, args);

    if (result instanceof RubyNil) {
        return null;
    } else if (result instanceof RubySymbol) {
        return result.asJavaString();
    } else {
        return result.asJavaString();
    }
}
 
Example #6
Source File: RubyObjectWrapper.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
public String getSymbol(String propertyName, Object... args) {
    IRubyObject result = getRubyProperty(propertyName, args);

    if (result instanceof RubyNil) {
        return null;
    }
    return result.asJavaString();
}
 
Example #7
Source File: RubyObjectWrapper.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
public boolean getBoolean(String propertyName, Object... args) {
    IRubyObject result = getRubyProperty(propertyName, args);
    if (result instanceof RubyNil) {
        return false;
    } else {
        return result.isTrue();
    }
}
 
Example #8
Source File: RubyObjectWrapper.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
public int getInt(String propertyName, Object... args) {
    IRubyObject result = getRubyProperty(propertyName, args);
    if (result instanceof RubyNil) {
        return 0;
    } else {
        return (int) ((RubyNumeric) result).getLongValue();
    }
}
 
Example #9
Source File: RubyObjectWrapper.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
public <T> List<T> getList(String propertyName, Class<T> elementClass, Object... args) {
    IRubyObject result = getRubyProperty(propertyName, args);
    if (result instanceof RubyNil) {
        return null;
    } else {
        List<T> ret = new ArrayList<T>();
        RubyArray array = (RubyArray) result;
        for (int i = 0; i < array.size(); i++) {
            ret.add(RubyUtils.rubyToJava(runtime, array.at(RubyFixnum.newFixnum(runtime, i)), elementClass));
        }
        return ret;
    }
}
 
Example #10
Source File: PigJrubyLibrary.java    From spork with Apache License 2.0 2 votes vote down vote up
/**
 * A type specific conversion routine.
 *
 * @param  rbObject object to convert
 * @return          analogous Pig type
 */
public static Object rubyToPig(RubyNil rbObject) {
    return null;
}