本文整理汇总了Java中org.jruby.runtime.Block类的典型用法代码示例。如果您正苦于以下问题:Java Block类的具体用法?Java Block怎么用?Java Block使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Block类属于org.jruby.runtime包,在下文中一共展示了Block类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: resultsCapturesJavaError
import org.jruby.runtime.Block; //导入依赖的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: hookIntoRuntime
import org.jruby.runtime.Block; //导入依赖的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();
}
});
}
示例3: instantiateClass
import org.jruby.runtime.Block; //导入依赖的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;
}
示例4: each
import org.jruby.runtime.Block; //导入依赖的package包/类
/**
* 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;
}
示例5: flatten
import org.jruby.runtime.Block; //导入依赖的package包/类
/**
* 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;
}
示例6: newThisType
import org.jruby.runtime.Block; //导入依赖的package包/类
private RubyMap newThisType(ThreadContext context) {
RubyMap newMap;
if (needTypeclass(valueType)) {
newMap = (RubyMap) metaClass.newInstance(context,
Utils.fieldTypeToRuby(context, keyType),
Utils.fieldTypeToRuby(context, valueType),
valueTypeClass, Block.NULL_BLOCK);
} else {
newMap = (RubyMap) metaClass.newInstance(context,
Utils.fieldTypeToRuby(context, keyType),
Utils.fieldTypeToRuby(context, valueType),
Block.NULL_BLOCK);
}
newMap.table = new HashMap<IRubyObject, IRubyObject>();
return newMap;
}
示例7: newMapForField
import org.jruby.runtime.Block; //导入依赖的package包/类
private RubyMap newMapForField(ThreadContext context, Descriptors.FieldDescriptor fieldDescriptor) {
RubyDescriptor mapDescriptor = (RubyDescriptor) getDescriptorForField(context, fieldDescriptor);
Descriptors.FieldDescriptor keyField = fieldDescriptor.getMessageType().findFieldByNumber(1);
Descriptors.FieldDescriptor valueField = fieldDescriptor.getMessageType().findFieldByNumber(2);
IRubyObject keyType = RubySymbol.newSymbol(context.runtime, keyField.getType().name());
IRubyObject valueType = RubySymbol.newSymbol(context.runtime, valueField.getType().name());
if (valueField.getType() == Descriptors.FieldDescriptor.Type.MESSAGE) {
RubyFieldDescriptor rubyFieldDescriptor = (RubyFieldDescriptor) mapDescriptor.lookup(context,
context.runtime.newString("value"));
RubyDescriptor rubyDescriptor = (RubyDescriptor) rubyFieldDescriptor.getSubType(context);
return (RubyMap) cMap.newInstance(context, keyType, valueType,
rubyDescriptor.msgclass(context), Block.NULL_BLOCK);
} else {
return (RubyMap) cMap.newInstance(context, keyType, valueType, Block.NULL_BLOCK);
}
}
示例8: msgdefCreateField
import org.jruby.runtime.Block; //导入依赖的package包/类
public static RubyFieldDescriptor msgdefCreateField(ThreadContext context, String label, IRubyObject name,
IRubyObject type, IRubyObject number, IRubyObject typeClass, RubyClass cFieldDescriptor) {
Ruby runtime = context.runtime;
RubyFieldDescriptor fieldDef = (RubyFieldDescriptor) cFieldDescriptor.newInstance(context, Block.NULL_BLOCK);
fieldDef.setLabel(context, runtime.newString(label));
fieldDef.setName(context, name);
fieldDef.setType(context, type);
fieldDef.setNumber(context, number);
if (!typeClass.isNil()) {
if (!(typeClass instanceof RubyString)) {
throw runtime.newArgumentError("expected string for type class");
}
fieldDef.setSubmsgName(context, typeClass);
}
return fieldDef;
}
示例9: each_pair
import org.jruby.runtime.Block; //导入依赖的package包/类
@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;
}
示例10: each
import org.jruby.runtime.Block; //导入依赖的package包/类
@JRubyMethod
public IRubyObject each(ThreadContext context, Block block) {
if (!block.isGiven()) return enumeratorize(context.runtime, this, "each");
for(int i=0; i < cnt; i++) {
block.yield(context, nth(context, RubyFixnum.newFixnum(context.runtime, i)));
}
return this;
}
示例11: collect
import org.jruby.runtime.Block; //导入依赖的package包/类
@JRubyMethod(name = {"collect", "map"})
public IRubyObject collect(ThreadContext context, Block block) {
Ruby runtime = context.runtime;
if (!block.isGiven()) return emptyVector(context, getMetaClass());
TransientVector ret = emptyVector(context, getMetaClass()).asTransient(context);
for (int i = 0; i < cnt; i++) {
ret = (TransientVector) ret.conj(context, block.yield(context, get(context, i)));
}
return ret.persistent(context, getMetaClass());
}
示例12: selectCommon
import org.jruby.runtime.Block; //导入依赖的package包/类
public IRubyObject selectCommon(ThreadContext context, Block block) {
Ruby runtime = context.runtime;
TransientVector ret = emptyVector(context, getMetaClass()).asTransient(context);
for (int i = 0; i < cnt; i++) {
IRubyObject value = get(context, i);
if (block.yield(context, value).isTrue()) ret = (TransientVector) ret.conj(context, value);
}
return ret.persistent(context, getMetaClass());
}
示例13: rejectCommon
import org.jruby.runtime.Block; //导入依赖的package包/类
public IRubyObject rejectCommon(ThreadContext context, Block block) {
Ruby runtime = context.runtime;
TransientVector ret = emptyVector(context, getMetaClass()).asTransient(context);
for (int i = 0; i < cnt; i++) {
IRubyObject value = get(context, i);
if (block.yield(context, value).isTrue()) continue;
ret = (TransientVector) ret.conj(context, value);
}
return ret.persistent(context, getMetaClass());
}
示例14: set
import org.jruby.runtime.Block; //导入依赖的package包/类
/**
* 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;
}
示例15: each
import org.jruby.runtime.Block; //导入依赖的package包/类
@JRubyMethod
public IRubyObject each(ThreadContext context, Block block) {
for (IRubyObject key : table.keySet()) {
block.yieldSpecific(context, key, table.get(key));
}
return context.runtime.getNil();
}