org.jruby.runtime.Block Java Examples
The following examples show how to use
org.jruby.runtime.Block.
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: TextAreaReadline.java From ramus with GNU General Public License v3.0 | 6 votes |
/** * 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(); } }); }
Example #2
Source File: ScriptUtils.java From APICloud-Studio with GNU General Public License v3.0 | 6 votes |
/** * 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; }
Example #3
Source File: ScriptUtils.java From APICloud-Studio with GNU General Public License v3.0 | 6 votes |
/** * instantiateClass * * @param runtime * @param module * @param name * @param args * @return */ public static IRubyObject instantiateClass(Ruby runtime, String module, String name, IRubyObject... args) { ThreadContext threadContext = runtime.getCurrentContext(); IRubyObject result = null; // try to load the module RubyModule rubyModule = runtime.getModule(module); if (rubyModule != null) { // now try to load the class RubyClass rubyClass = rubyModule.getClass(name); // instantiate it, if it exists if (rubyClass != null) { result = rubyClass.newInstance(threadContext, args, Block.NULL_BLOCK); } } return result; }
Example #4
Source File: RubyScriptTest.java From marathonv5 with Apache License 2.0 | 6 votes |
@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); }
Example #5
Source File: RubyDataBag.java From spork with Apache License 2.0 | 6 votes |
/** * This is an implementation of the each method which opens up the Enumerable interface, * and makes it very convenient to iterate over the elements of a DataBag. Note that currently, * due to a deficiency in JRuby, it is not possible to call each without a block given. * * @param context the context the method is being executed in * @param block a block to call on the elements of the bag * @return enumerator object if null block given, nil otherwise */ @JRubyMethod public IRubyObject each(ThreadContext context, Block block) throws ExecException{ Ruby runtime = context.getRuntime(); if (!block.isGiven()) return PigJrubyLibrary.enumeratorize(runtime, this, "each"); /* In a future release of JRuby when enumeratorize is made public (which is planned), should replace the above with the below if (!block.isGiven()) return RubyEnumerator.enumeratorize(context.getRuntime(), this, "each"); */ for (Tuple t : this) block.yield(context, PigJrubyLibrary.pigToRuby(runtime, t)); return context.nil; }
Example #6
Source File: RubyDataBag.java From spork with Apache License 2.0 | 6 votes |
/** * This is a convenience method which will run the given block on the first element * of each tuple contained. * * @param context the context the method is being executed in * @param block a block to call on the elements of the bag * @return enumerator object if null block given, nil otherwise */ @JRubyMethod(name = {"flat_each", "flatten"}) public IRubyObject flatten(ThreadContext context, Block block) throws ExecException { Ruby runtime = context.getRuntime(); if (!block.isGiven()) return PigJrubyLibrary.enumeratorize(runtime, this, "flatten"); /* In a future release of JRuby when enumeratorize is made public (which is planned), should replace the above with the below if (!block.isGiven()) return RubyEnumerator.enumeratorize(context.getRuntime(), this, "flatten"); */ for (Tuple t : this) block.yield(context, PigJrubyLibrary.pigToRuby(runtime, t.get(0))); return context.nil; }
Example #7
Source File: JavaLogger.java From asciidoctorj with Apache License 2.0 | 6 votes |
/** * @param threadContext * @param args */ @JRubyMethod(name = "add", required = 1, optional = 2) public IRubyObject add(final ThreadContext threadContext, final IRubyObject[] args, Block block) { final IRubyObject rubyMessage; if (args.length >= 2 && !args[1].isNil()) { rubyMessage = args[1]; } else if (block.isGiven()) { rubyMessage = block.yield(threadContext, getRuntime().getNil()); } else { rubyMessage = args[2]; } final Cursor cursor = getSourceLocation(rubyMessage); final String message = formatMessage(rubyMessage); final Severity severity = mapRubyLogLevel(args[0]); final LogRecord record = createLogRecord(threadContext, severity, cursor, message); rootLogHandler.log(record); return getRuntime().getNil(); }
Example #8
Source File: JRubyRackInput.java From rack-servlet with Apache License 2.0 | 5 votes |
@JRubyMethod public IRubyObject each(ThreadContext context, Block block) { IRubyObject nil = getRuntime().getNil(); IRubyObject line; while ((line = gets()) != nil) { block.yield(context, line); } return nil; }
Example #9
Source File: ExtensionGroupImpl.java From asciidoctorj with Apache License 2.0 | 5 votes |
@Override public void register() { IRubyObject callback = extensionGroupClass.newInstance(rubyRuntime.getCurrentContext(), Block.NULL_BLOCK); asciidoctorModule.callMethod("register_extension_group", rubyRuntime.newString(this.groupName), callback, JavaEmbedUtils.javaToRuby(rubyRuntime, registrators)); }
Example #10
Source File: JavaLogger.java From asciidoctorj with Apache License 2.0 | 5 votes |
private IRubyObject log(ThreadContext threadContext, IRubyObject[] args, Block block, Severity severity) { final IRubyObject rubyMessage; if (block.isGiven()) { rubyMessage = block.yield(threadContext, getRuntime().getNil()); } else { rubyMessage = args[0]; } final Cursor cursor = getSourceLocation(rubyMessage); final String message = formatMessage(rubyMessage); final LogRecord record = createLogRecord(threadContext, severity, cursor, message); rootLogHandler.log(record); return getRuntime().getNil(); }
Example #11
Source File: JavaLogger.java From asciidoctorj with Apache License 2.0 | 5 votes |
@JRubyMethod(name = "initialize", required = 1, optional = 2) public IRubyObject initialize(final ThreadContext threadContext, final IRubyObject[] args) { return Helpers.invokeSuper( threadContext, this, getMetaClass(), "initialize", args, Block.NULL_BLOCK); }
Example #12
Source File: TreeprocessorProxy.java From asciidoctorj with Apache License 2.0 | 5 votes |
@JRubyMethod(name = "initialize", required = 1) public IRubyObject initialize(ThreadContext context, IRubyObject options) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { if (getProcessor() != null) { // Instance was created in Java and has options set, so we pass these // instead of those passed by asciidoctor Helpers.invokeSuper( context, this, getMetaClass(), METHOD_NAME_INITIALIZE, new IRubyObject[]{ RubyHashUtil.convertMapToRubyHashWithSymbols(getRuntime(), getProcessor().getConfig())}, Block.NULL_BLOCK); // The extension config in the Java extension is just a view on the @config member of the Ruby part getProcessor().updateConfig(new RubyHashMapDecorator((RubyHash) getInstanceVariable(MEMBER_NAME_CONFIG))); } else { // First create only the instance passing in the block name setProcessor(instantiateProcessor(new HashMap<String, Object>())); // Then create the config hash that may contain config options defined in the Java constructor RubyHash config = RubyHashUtil.convertMapToRubyHashWithSymbols(context.getRuntime(), getProcessor().getConfig()); // Initialize the Ruby part and pass in the config options Helpers.invokeSuper(context, this, getMetaClass(), METHOD_NAME_INITIALIZE, new IRubyObject[]{config}, Block.NULL_BLOCK); // Reset the Java config options to the decorated Ruby hash, so that Java and Ruby work on the same config map getProcessor().setConfig(new RubyHashMapDecorator((RubyHash) getInstanceVariable(MEMBER_NAME_CONFIG))); } finalizeJavaConfig(); return null; }
Example #13
Source File: IncludeProcessorProxy.java From asciidoctorj with Apache License 2.0 | 5 votes |
@JRubyMethod(name = "initialize", required = 1) public IRubyObject initialize(ThreadContext context, IRubyObject options) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { if (getProcessor() != null) { // Instance was created in Java and has options set, so we pass these // instead of those passed by asciidoctor Helpers.invokeSuper( context, this, getMetaClass(), METHOD_NAME_INITIALIZE, new IRubyObject[]{ RubyHashUtil.convertMapToRubyHashWithSymbols(getRuntime(), getProcessor().getConfig())}, Block.NULL_BLOCK); // The extension config in the Java extension is just a view on the @config member of the Ruby part getProcessor().updateConfig(new RubyHashMapDecorator((RubyHash) getInstanceVariable(MEMBER_NAME_CONFIG))); } else { // First create only the instance passing in the block name setProcessor(instantiateProcessor(new HashMap<String, Object>())); // Then create the config hash that may contain config options defined in the Java constructor RubyHash config = RubyHashUtil.convertMapToRubyHashWithSymbols(context.getRuntime(), getProcessor().getConfig()); // Initialize the Ruby part and pass in the config options Helpers.invokeSuper(context, this, getMetaClass(), METHOD_NAME_INITIALIZE, new IRubyObject[] {config}, Block.NULL_BLOCK); // Reset the Java config options to the decorated Ruby hash, so that Java and Ruby work on the same config map getProcessor().setConfig(new RubyHashMapDecorator((RubyHash) getInstanceVariable(MEMBER_NAME_CONFIG))); } finalizeJavaConfig(); return null; }
Example #14
Source File: DocinfoProcessorProxy.java From asciidoctorj with Apache License 2.0 | 5 votes |
@JRubyMethod(name = "initialize", required = 1) public IRubyObject initialize(ThreadContext context, IRubyObject options) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { if (getProcessor() != null) { // Instance was created in Java and has options set, so we pass these // instead of those passed by asciidoctor Helpers.invokeSuper( context, this, getMetaClass(), METHOD_NAME_INITIALIZE, new IRubyObject[]{RubyHashUtil.convertMapToRubyHashWithSymbols(getRuntime(), getProcessor().getConfig())}, Block.NULL_BLOCK); // The extension config in the Java extension is just a view on the @config member of the Ruby part getProcessor().updateConfig(new RubyHashMapDecorator((RubyHash) getInstanceVariable(MEMBER_NAME_CONFIG))); } else { // First create only the instance passing in the block name setProcessor(instantiateProcessor(new HashMap<String, Object>())); // Then create the config hash that may contain config options defined in the Java constructor RubyHash config = RubyHashUtil.convertMapToRubyHashWithSymbols(context.getRuntime(), getProcessor().getConfig()); // Initialize the Ruby part and pass in the config options Helpers.invokeSuper(context, this, getMetaClass(), METHOD_NAME_INITIALIZE, new IRubyObject[] {config}, Block.NULL_BLOCK); // Reset the Java config options to the decorated Ruby hash, so that Java and Ruby work on the same config map getProcessor().setConfig(new RubyHashMapDecorator((RubyHash) getInstanceVariable(MEMBER_NAME_CONFIG))); } finalizeJavaConfig(); return null; }
Example #15
Source File: PostprocessorProxy.java From asciidoctorj with Apache License 2.0 | 5 votes |
@JRubyMethod(name = "initialize", required = 1) public IRubyObject initialize(ThreadContext context, IRubyObject options) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { if (getProcessor() != null) { // Instance was created in Java and has options set, so we pass these // instead of those passed by asciidoctor Helpers.invokeSuper( context, this, getMetaClass(), METHOD_NAME_INITIALIZE, new IRubyObject[]{ RubyHashUtil.convertMapToRubyHashWithSymbols(getRuntime(), getProcessor().getConfig())}, Block.NULL_BLOCK); // The extension config in the Java extension is just a view on the @config member of the Ruby part getProcessor().updateConfig(new RubyHashMapDecorator((RubyHash) getInstanceVariable(MEMBER_NAME_CONFIG))); } else { // First create only the instance passing in the block name setProcessor(instantiateProcessor(new HashMap<String, Object>())); // Then create the config hash that may contain config options defined in the Java constructor RubyHash config = RubyHashUtil.convertMapToRubyHashWithSymbols(context.getRuntime(), getProcessor().getConfig()); // Initialize the Ruby part and pass in the config options Helpers.invokeSuper(context, this, getMetaClass(), METHOD_NAME_INITIALIZE, new IRubyObject[] {config}, Block.NULL_BLOCK); // Reset the Java config options to the decorated Ruby hash, so that Java and Ruby work on the same config map getProcessor().setConfig(new RubyHashMapDecorator((RubyHash) getInstanceVariable(MEMBER_NAME_CONFIG))); } finalizeJavaConfig(); return null; }
Example #16
Source File: PreprocessorProxy.java From asciidoctorj with Apache License 2.0 | 5 votes |
@JRubyMethod(name = "initialize", required = 1) public IRubyObject initialize(ThreadContext context, IRubyObject options) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { if (getProcessor() != null) { // Instance was created in Java and has options set, so we pass these // instead of those passed by asciidoctor Helpers.invokeSuper( context, this, getMetaClass(), "initialize", new IRubyObject[]{ JavaEmbedUtils.javaToRuby(getRuntime(), getProcessor().getConfig()) }, Block.NULL_BLOCK); } else { // First create only the instance passing in the block name setProcessor(instantiateProcessor(new HashMap<String, Object>())); // Then create the config hash that may contain config options defined in the Java constructor RubyHash config = RubyHashUtil.convertMapToRubyHashWithSymbols(context.getRuntime(), getProcessor().getConfig()); // Initialize the Ruby part and pass in the config options Helpers.invokeSuper(context, this, getMetaClass(), METHOD_NAME_INITIALIZE, new IRubyObject[]{config}, Block.NULL_BLOCK); // Reset the Java config options to the decorated Ruby hash, so that Java and Ruby work on the same config map getProcessor().setConfig(new RubyHashMapDecorator((RubyHash) getInstanceVariable(MEMBER_NAME_CONFIG))); } finalizeJavaConfig(); return null; }
Example #17
Source File: RubySchema.java From spork with Apache License 2.0 | 5 votes |
/** * This allows the users to set an index or a range of values to * a specified RubySchema. The first argument must be a Fixnum or Range, * and the second argument may optionally be a Fixnum. The given index * (or range of indices) will be replaced by a RubySchema instantiated * based on the remaining arguments. * * @param context the contextthe method is being executed in * @param args a varargs which has to be at least length two. * @return the RubySchema that was added */ @JRubyMethod(name = {"[]=", "set"}, required = 2, rest = true) public RubySchema set(ThreadContext context, IRubyObject[] args) { IRubyObject arg1 = args[0]; IRubyObject arg2 = args[1]; IRubyObject[] arg3 = Arrays.copyOfRange(args, 1, args.length); Schema s = internalSchema; Ruby runtime = context.getRuntime(); List<Schema.FieldSchema> lfs = s.getFields(); int min, max; if (arg1 instanceof RubyFixnum && arg2 instanceof RubyFixnum) { min = (int)((RubyFixnum)arg1).getLongValue(); max = (int)((RubyFixnum)arg2).getLongValue(); arg3 = Arrays.copyOfRange(args, 2, args.length); } else if (arg1 instanceof RubyFixnum) { min = (int)((RubyFixnum)arg1).getLongValue(); max = min + 1; } else if (arg1 instanceof RubyRange) { min = (int)((RubyFixnum)((RubyRange)arg1).min(context, Block.NULL_BLOCK)).getLongValue(); max = (int)((RubyFixnum)((RubyRange)arg1).max(context, Block.NULL_BLOCK)).getLongValue() + 1; } else { throw new RuntimeException("Bad arguments given to get function: ( " + arg1.toString() + " , " + arg2.toString()+ " )"); } for (int i = min; i < max; i++) lfs.remove(min); if (arg3 == null || arg3.length == 0) throw new RuntimeException("Must have schema argument for []="); RubySchema rs = new RubySchema(runtime, runtime.getClass("Schema")).initialize(arg3); for (Schema.FieldSchema fs : rs.getInternalSchema().getFields()) lfs.add(min++, fs); RubySchema.fixSchemaNames(internalSchema); return rs; }
Example #18
Source File: JRubyCacheBackendLibrary.java From thread_safe with Apache License 2.0 | 5 votes |
@JRubyMethod public IRubyObject each_pair(ThreadContext context, Block block) { for (Map.Entry<IRubyObject,IRubyObject> entry : map.entrySet()) { block.yieldSpecific(context, entry.getKey(), entry.getValue()); } return this; }
Example #19
Source File: JavaLogger.java From asciidoctorj with Apache License 2.0 | 4 votes |
@JRubyMethod(name = "fatal", required = 0, optional = 2) public IRubyObject fatal(final ThreadContext threadContext, final IRubyObject[] args, Block block) { return log(threadContext, args, block, Severity.FATAL); }
Example #20
Source File: JavaLogger.java From asciidoctorj with Apache License 2.0 | 4 votes |
@JRubyMethod(name = "error", required = 0, optional = 2) public IRubyObject error(final ThreadContext threadContext, final IRubyObject[] args, Block block) { return log(threadContext, args, block, Severity.ERROR); }
Example #21
Source File: JavaLogger.java From asciidoctorj with Apache License 2.0 | 4 votes |
@JRubyMethod(name = "warn", required = 0, optional = 2) public IRubyObject warn(final ThreadContext threadContext, final IRubyObject[] args, Block block) { return log(threadContext, args, block, Severity.WARN); }
Example #22
Source File: JavaLogger.java From asciidoctorj with Apache License 2.0 | 4 votes |
@JRubyMethod(name = "info", required = 0, optional = 2) public IRubyObject info(final ThreadContext threadContext, final IRubyObject[] args, Block block) { return log(threadContext, args, block, Severity.INFO); }
Example #23
Source File: JavaLogger.java From asciidoctorj with Apache License 2.0 | 4 votes |
@JRubyMethod(name = "debug", required = 0, optional = 2) public IRubyObject debug(final ThreadContext threadContext, final IRubyObject[] args, Block block) { return log(threadContext, args, block, Severity.DEBUG); }