本文整理汇总了Java中org.jruby.runtime.builtin.IRubyObject类的典型用法代码示例。如果您正苦于以下问题:Java IRubyObject类的具体用法?Java IRubyObject怎么用?Java IRubyObject使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
IRubyObject类属于org.jruby.runtime.builtin包,在下文中一共展示了IRubyObject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: resultsCapturesJavaError
import org.jruby.runtime.builtin.IRubyObject; //导入依赖的package包/类
@Test(enabled = false) public void resultsCapturesJavaError() throws Exception {
RubyScript script = new RubyScript(out, err, converToCode(SCRIPT_CONTENTS_ERROR_FROM_JAVA),
new File(System.getProperty(Constants.PROP_PROJECT_DIR), "dummyfile.rb").getAbsolutePath(), false, null,
Constants.FRAMEWORK_SWING);
script.setDriverURL("");
Ruby interpreter = script.getInterpreter();
assertTrue("Collector not defined", interpreter.isClassDefined("Collector"));
RubyClass collectorClass = interpreter.getClass("Collector");
IRubyObject presult = JavaEmbedUtils.javaToRuby(interpreter, result);
IRubyObject collector = collectorClass.newInstance(interpreter.getCurrentContext(), new IRubyObject[0], new Block(null));
IRubyObject rubyObject = interpreter.evalScriptlet("proc { my_function }");
try {
collector.callMethod(interpreter.getCurrentContext(), "callprotected", new IRubyObject[] { rubyObject, presult });
} catch (Throwable t) {
}
assertEquals(1, result.failureCount());
Failure[] failures = result.failures();
assertTrue("Should end with TestRubyScript.java. but has " + failures[0].getTraceback()[0].fileName,
failures[0].getTraceback()[0].fileName.endsWith("TestRubyScript.java"));
assertEquals("throwError", failures[0].getTraceback()[0].functionName);
}
示例2: createJRubyObject
import org.jruby.runtime.builtin.IRubyObject; //导入依赖的package包/类
/**
* 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));
}
示例3: invoke
import org.jruby.runtime.builtin.IRubyObject; //导入依赖的package包/类
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (ReflectionUtils.isEqualsMethod(method)) {
return (isProxyForSameRubyObject(args[0]));
}
else if (ReflectionUtils.isHashCodeMethod(method)) {
return this.rubyObject.hashCode();
}
else if (ReflectionUtils.isToStringMethod(method)) {
String toStringResult = this.rubyObject.toString();
if (!StringUtils.hasText(toStringResult)) {
toStringResult = ObjectUtils.identityToString(this.rubyObject);
}
return "JRuby object [" + toStringResult + "]";
}
try {
IRubyObject[] rubyArgs = convertToRuby(args);
IRubyObject rubyResult =
this.rubyObject.callMethod(this.ruby.getCurrentContext(), method.getName(), rubyArgs);
return convertFromRuby(rubyResult, method.getReturnType());
}
catch (RaiseException ex) {
throw new JRubyExecutionException(ex);
}
}
示例4: hookIntoRuntime
import org.jruby.runtime.builtin.IRubyObject; //导入依赖的package包/类
/**
* Hooks this <code>TextAreaReadline</code> instance into the runtime,
* redefining the <code>Readline</code> module so that it uses this object.
* This method does not redefine the standard input-output streams. If you
* need that, use {@link #hookIntoRuntimeWithStreams(Ruby)}.
*
* @param runtime The runtime.
* @see #hookIntoRuntimeWithStreams(Ruby)
*/
public void hookIntoRuntime(final Ruby runtime) {
this.runtime = runtime;
/* Hack in to replace usual readline with this */
runtime.getLoadService().require("readline");
RubyModule readlineM = runtime.fastGetModule("Readline");
readlineM.defineModuleFunction("readline", new Callback() {
public IRubyObject execute(IRubyObject recv, IRubyObject[] args,
Block block) {
String line = readLine(args[0].toString());
if (line != null) {
return RubyString.newUnicodeString(runtime, line);
} else {
return runtime.getNil();
}
}
public Arity getArity() {
return Arity.twoArguments();
}
});
}
示例5: evaluate
import org.jruby.runtime.builtin.IRubyObject; //导入依赖的package包/类
public Object evaluate(final String expression, final Map<String, ?> values) throws ExpressionEvaluationException {
LOG.debug("Evaluating JRuby expression: {1}", expression);
try {
final RubyInstanceConfig config = new RubyInstanceConfig();
config.setCompatVersion(CompatVersion.RUBY1_9);
final Ruby runtime = JavaEmbedUtils.initialize(new ArrayList<String>(), config);
final StringBuilder localVars = new StringBuilder();
for (final Entry<String, ?> entry : values.entrySet()) {
runtime.getGlobalVariables().set("$" + entry.getKey(), JavaEmbedUtils.javaToRuby(runtime, entry.getValue()));
localVars.append(entry.getKey()) //
.append("=$") //
.append(entry.getKey()) //
.append("\n");
}
final IRubyObject result = runtime.evalScriptlet(localVars + expression);
return JavaEmbedUtils.rubyToJava(runtime, result, Object.class);
} catch (final RuntimeException ex) {
throw new ExpressionEvaluationException("Evaluating JRuby expression failed: " + expression, ex);
}
}
示例6: getValueFromView
import org.jruby.runtime.builtin.IRubyObject; //导入依赖的package包/类
public Object getValueFromView(Object view, String[] path) {
if(view instanceof IRubyObject) {
IRubyObject o = (IRubyObject)view;
for(String key : path) {
if(o == null || o.isNil()) { return null; }
o = o.callMethod(o.getRuntime().getCurrentContext(), "[]",
RubyString.newUnicodeString(o.getRuntime(), key));
}
if(o instanceof ConcreteJavaProxy) {
return ((ConcreteJavaProxy)o).getObject();
}
return ((o == null) || o.isNil()) ? null : o;
} else if(path.length == 0) {
return view; // to allow . value to work
}
return null;
}
示例7: valueIsTruthy
import org.jruby.runtime.builtin.IRubyObject; //导入依赖的package包/类
public boolean valueIsTruthy(Object value) {
if(value instanceof IRubyObject) {
IRubyObject o = (IRubyObject)value;
if(o == null) {
return false;
} else if(o instanceof RubyString) {
return !(((RubyString)o).isEmpty());
} else if(o instanceof RubyBoolean) {
return (o instanceof RubyBoolean.True);
} else if(o instanceof RubyArray) {
return !(((RubyArray)o).isEmpty());
} else if(value instanceof RubyNumeric) {
return ((RubyNumeric)value).getDoubleValue() != 0.0;
}
return false;
} else {
return super.valueIsTruthy(value);
}
}
示例8: enter
import org.jruby.runtime.builtin.IRubyObject; //导入依赖的package包/类
@Override
public Object enter( String entryPointName, Executable executable, ExecutionContext executionContext, Object... arguments ) throws NoSuchMethodException, ParsingException, ExecutionException
{
entryPointName = toRubyStyle( entryPointName );
Ruby rubyRuntime = getRubyRuntime( executionContext );
// Note that we're using the shared compilerRuntime to do the
// conversions, so that we can cache Java proxies. It's very, very
// slow if we have to generate them on the fly every time!
IRubyObject[] rubyArguments = JavaUtil.convertJavaArrayToRuby( compilerRuntime, arguments );
try
{
IRubyObject value = rubyRuntime.getTopSelf().callMethod( rubyRuntime.getCurrentContext(), entryPointName, rubyArguments );
return value.toJava( Object.class );
}
catch( RaiseException x )
{
throw createExecutionException( x );
}
finally
{
rubyRuntime.getOut().flush();
rubyRuntime.getErr().flush();
}
}
示例9: setConsole
import org.jruby.runtime.builtin.IRubyObject; //导入依赖的package包/类
/**
* setConsole
*
* @param ostream
*/
protected IRubyObject setConsole(OutputStream ostream)
{
Ruby runtime = getRuntime();
RubyClass object = runtime.getObject();
IRubyObject oldValue = null;
// CONSOLE will only exist already if one command invokes another
if (object.hasConstant(CONSOLE_CONSTANT))
{
oldValue = object.getConstant(CONSOLE_CONSTANT);
}
if (ostream != null)
{
RubyIO io = new RubyIO(runtime, ostream);
io.getOpenFile().getMainStream().setSync(true);
setConsole(io);
}
return oldValue;
}
示例10: setErrorWriter
import org.jruby.runtime.builtin.IRubyObject; //导入依赖的package包/类
/**
* setErrorWriter - based on ScriptContainer#setErrorWriter
*
* @param ostream
*/
protected IRubyObject setErrorWriter(OutputStream ostream)
{
Ruby runtime = getRuntime();
IRubyObject oldValue = runtime.getObject().getConstant(STDERR_CONSTANT);
if (ostream != null)
{
PrintStream pstream = new PrintStream(ostream);
RubyIO io = new RubyIO(runtime, pstream);
io.getOpenFile().getMainStream().setSync(true);
setErrorWriter(io);
}
return oldValue;
}
示例11: setWriter
import org.jruby.runtime.builtin.IRubyObject; //导入依赖的package包/类
/**
* setWriter - based on ScriptContainer#setWriter
*
* @param ostream
*/
protected IRubyObject setWriter(OutputStream ostream)
{
Ruby runtime = this.getRuntime();
IRubyObject oldValue = runtime.getObject().getConstant(STDOUT_CONSTANT);
if (ostream != null)
{
PrintStream pstream = new PrintStream(ostream);
RubyIO io = new RubyIO(runtime, pstream);
io.getOpenFile().getMainStream().setSync(true);
setWriter(io);
}
return oldValue;
}
示例12: unapplyEnvironment
import org.jruby.runtime.builtin.IRubyObject; //导入依赖的package包/类
/**
* unapplyEnvironment
*/
protected void unapplyEnvironment()
{
Ruby runtime = this.getRuntime();
IRubyObject env = runtime.getObject().getConstant(ENV_PROPERTY);
if (env != null && env instanceof RubyHash)
{
RubyHash hash = (RubyHash) env;
// restore original content
hash.replace(runtime.getCurrentContext(), this._originalEnvironment);
// lose reference
this._originalEnvironment = null;
}
}
示例13: applyLoadPaths
import org.jruby.runtime.builtin.IRubyObject; //导入依赖的package包/类
/**
* applyLoadPaths
*
* @param container
*/
protected void applyLoadPaths(Ruby runtime)
{
if (this._loadPaths != null && this._loadPaths.size() > 0)
{
IRubyObject object = runtime.getLoadService().getLoadPath();
if (object instanceof RubyArray)
{
RubyArray loadPathArray = (RubyArray) object;
// save copy for later
this._originalLoadPaths = (RubyArray) loadPathArray.dup();
// Add our custom load paths
for (String loadPath : this._loadPaths)
{
RubyString toAdd = runtime.newString(loadPath.replace('\\', '/'));
loadPathArray.append(toAdd);
}
}
}
}
示例14: unapplyLoadPaths
import org.jruby.runtime.builtin.IRubyObject; //导入依赖的package包/类
/**
* unapplyLoadPaths
*
* @param runtime
*/
protected void unapplyLoadPaths(Ruby runtime)
{
if (this._loadPaths != null && this._loadPaths.size() > 0)
{
IRubyObject object = runtime.getLoadService().getLoadPath();
if (object != null && object instanceof RubyArray)
{
RubyArray loadPathArray = (RubyArray) object;
// Restore original content
loadPathArray.replace(this._originalLoadPaths);
// lose reference
this._originalLoadPaths = null;
}
}
}
示例15: instantiateClass
import org.jruby.runtime.builtin.IRubyObject; //导入依赖的package包/类
/**
* instantiateClass
*
* @param runtime
* @param name
* @param args
* @return
*/
public static IRubyObject instantiateClass(Ruby runtime, String name, IRubyObject... args)
{
ThreadContext threadContext = runtime.getCurrentContext();
IRubyObject result = null;
// try to load the class
RubyClass rubyClass = runtime.getClass(name);
// instantiate it, if it exists
if (rubyClass != null)
{
result = rubyClass.newInstance(threadContext, args, Block.NULL_BLOCK);
}
return result;
}