本文整理汇总了Java中org.jruby.Ruby.evalScriptlet方法的典型用法代码示例。如果您正苦于以下问题:Java Ruby.evalScriptlet方法的具体用法?Java Ruby.evalScriptlet怎么用?Java Ruby.evalScriptlet使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.jruby.Ruby
的用法示例。
在下文中一共展示了Ruby.evalScriptlet方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getInitRuby
import org.jruby.Ruby; //导入方法依赖的package包/类
protected Callable<Ruby> getInitRuby(final Writer out, final Writer err) {
return new Callable<Ruby>() {
@Override public Ruby call() throws Exception {
RubyInstanceConfig config = new RubyInstanceConfig();
config.setCompileMode(CompileMode.OFF);
List<String> loadPaths = new ArrayList<String>();
setModule(loadPaths);
String appRubyPath = System.getProperty(PROP_APPLICATION_RUBYPATH);
if (appRubyPath != null) {
StringTokenizer tok = new StringTokenizer(appRubyPath, ";");
while (tok.hasMoreTokens()) {
loadPaths.add(tok.nextToken().replace('/', File.separatorChar));
}
}
config.setOutput(new PrintStream(new WriterOutputStream(out)));
config.setError(new PrintStream(new WriterOutputStream(err)));
Ruby interpreter = JavaEmbedUtils.initialize(loadPaths, config);
interpreter.evalScriptlet("require 'selenium/webdriver'");
interpreter.evalScriptlet("require 'marathon/results'");
interpreter.evalScriptlet("require 'marathon/playback-" + framework + "'");
return interpreter;
}
};
}
示例2: resultsCapturesJavaError
import org.jruby.Ruby; //导入方法依赖的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);
}
示例3: createJRubyObject
import org.jruby.Ruby; //导入方法依赖的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));
}
示例4: run
import org.jruby.Ruby; //导入方法依赖的package包/类
private void run(final Ruby runtime) {
Thread t2 = new Thread() {
public void run() {
try {
InputStream is = getClass().getResourceAsStream("/console_irb.rb");
ByteArrayOutputStream ba = new ByteArrayOutputStream();
int r;
while ((r = is.read()) >= 0)
ba.write(r);
is.close();
//runtime
// .evalScriptlet("require 'irb'\n require 'irb/completion'\n IRB.start(__FILE__)\n");
runtime.evalScriptlet(new String(ba.toByteArray()));
//run();
} catch (Exception e) {
e.printStackTrace();
}
}
};
t2.start();
}
示例5: evaluate
import org.jruby.Ruby; //导入方法依赖的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: createJRubyObject
import org.jruby.Ruby; //导入方法依赖的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
*/
@SuppressWarnings("deprecation")
public static Object createJRubyObject(String scriptSource, Class<?>[] interfaces, ClassLoader classLoader) {
Ruby ruby = initializeRuntime();
Node scriptRootNode = ruby.parseEval(scriptSource, "", null, 0);
// Keep using the deprecated runNormally variant for JRuby 1.1/1.2 compatibility...
IRubyObject rubyObject = ruby.runNormally(scriptRootNode, false);
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));
}
示例7: initDriftRuntime
import org.jruby.Ruby; //导入方法依赖的package包/类
/**
* TODO contribute back hooks for integration purposes Initializes the
* runtime (exports the context, boots the Rack handler).
*
* NOTE: (package) visible due specs
*
* @param runtime
*/
protected void initDriftRuntime(final Ruby runtime) {
// set $servlet_context :
runtime.getGlobalVariables().set("$servlet_context", JavaUtil.convertJavaToRuby(runtime, rackServletContext));
// load our (servlet) Rack handler :
runtime.evalScriptlet("require 'rack/handler/servlet'");
// NOTE: this is experimental stuff and might change in the future :
String env = rackServletContext.getConfig().getProperty("jruby.rack.handler.env");
// currently supported "env" values are 'default' and 'servlet'
if (env != null) {
runtime.evalScriptlet("Rack::Handler::Servlet.env = '" + env + "'");
}
String response = rackServletContext.getConfig().getProperty("jruby.rack.handler.response");
if (response == null) {
response = rackServletContext.getConfig().getProperty("jruby.rack.response");
}
if (response != null) { // JRuby::Rack::JettyResponse ->
// 'jruby/rack/jetty_response'
runtime.evalScriptlet("Rack::Handler::Servlet.response = '" + response + "'");
}
// configure (Ruby) bits and pieces :
String dechunk = rackServletContext.getConfig().getProperty("jruby.rack.response.dechunk");
Boolean dechunkFlag = (Boolean) DefaultRackConfig.toStrictBoolean(dechunk, null);
if (dechunkFlag != null) {
runtime.evalScriptlet("JRuby::Rack::Response.dechunk = " + dechunkFlag + "");
} else { // dechunk null (default) or not a true/false value ... we're
// patch :
runtime.evalScriptlet("JRuby::Rack::Booter.on_boot { require 'jruby/rack/chunked' }");
// `require 'jruby/rack/chunked'` that happens after Rack is loaded
}
String swallowAbort = rackServletContext.getConfig().getProperty("jruby.rack.response.swallow_client_abort");
Boolean swallowAbortFlag = (Boolean) DefaultRackConfig.toStrictBoolean(swallowAbort, null);
if (swallowAbortFlag != null) {
runtime.evalScriptlet("JRuby::Rack::Response.swallow_client_abort = " + swallowAbortFlag + "");
}
}
示例8: ContextAccessor
import org.jruby.Ruby; //导入方法依赖的package包/类
public ContextAccessor(IRubyObject o) {
this.o = o;
Ruby runtime = o.getRuntime();
marathon = runtime.evalScriptlet("$marathon");
}