本文整理汇总了Java中org.jruby.runtime.ThreadContext.getRuntime方法的典型用法代码示例。如果您正苦于以下问题:Java ThreadContext.getRuntime方法的具体用法?Java ThreadContext.getRuntime怎么用?Java ThreadContext.getRuntime使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.jruby.runtime.ThreadContext
的用法示例。
在下文中一共展示了ThreadContext.getRuntime方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: each
import org.jruby.runtime.ThreadContext; //导入方法依赖的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;
}
示例2: add
import org.jruby.runtime.ThreadContext; //导入方法依赖的package包/类
/**
* A ruby method that adds two numbers. In practice we would do some more
* complex operation in java possibly using a library method, or a pure java
* method of our own creation (possible private).
* @param context ThreadContext
* @param recv the receiver
* @param args array of input arguments
* @return The outcome of doing a plus b.
*/
@JRubyMethod(name = "add", module = true, rest = true)
public static IRubyObject add(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
Ruby runtime = context.getRuntime();
// Arity.checkArgumentCount(runtime, args, Arity.OPTIONAL.getValue(), 2);
int a = (int) args[0].toJava(Integer.class);
int b = (int) args[1].toJava(Integer.class);
int result = a + b;
return runtime.newFixnum(result);
}
示例3: mult
import org.jruby.runtime.ThreadContext; //导入方法依赖的package包/类
/**
* Multiplies two numbers (in practice you would implement some method in java,
* probably using an external library)
* @param context ThreadContext
* @param args the ruby way of coping with more than two arguments
* @return result probably RubyFixnum
*/
@JRubyMethod(name = "multiply", rest = true)
public IRubyObject mult(ThreadContext context, IRubyObject[] args) {
Ruby runtime = context.getRuntime();
// Arity.checkArgumentCount(runtime, args, Arity.OPTIONAL.getValue(), 2);
int a = (int) args[0].toJava(Integer.class);
int b = (int) args[1].toJava(Integer.class);
int result = a * b;
return runtime.newFixnum(result);
}
示例4: get
import org.jruby.runtime.ThreadContext; //导入方法依赖的package包/类
/**
* This is a version of [] which allows the range to be specified as such: [1,2].
*
* @param context the context the method is being executed in
* @param arg1 a Fixnum start index
* @param arg2 a Fixnum end index
* @return the RubySchema object encapsulated the found Schema
*/
@JRubyMethod(name = {"[]", "slice"})
public RubySchema get(ThreadContext context, IRubyObject arg1, IRubyObject arg2) {
if (arg1 instanceof RubyFixnum && arg2 instanceof RubyFixnum) {
Ruby runtime = context.getRuntime();
int min = (int)((RubyFixnum)arg1).getLongValue();
int max = (int)((RubyFixnum)arg2).getLongValue() - 1;
return new RubySchema(runtime, runtime.getClass("Schema"), new Schema(internalSchema.getFields().subList(min, max + 1)), false);
} else {
throw new RuntimeException("Bad arguments given to get function: ( " + arg1.toString() + " , " + arg2.toString()+ " )");
}
}
示例5: bag
import org.jruby.runtime.ThreadContext; //导入方法依赖的package包/类
/**
* This is a ruby method which takes an array of arguments and constructs a Bag schema from them. The name
* will be set automatically.
*
* @param context the context the method is being executed in
* @param self the RubyClass for the Class object this was invoked on
* @param arg a list of arguments to instantiate the new RubySchema
* @return the new RubySchema
*/
@JRubyMethod(meta = true, name = {"b", "bag"})
public static RubySchema bag(ThreadContext context, IRubyObject self, IRubyObject arg) {
Schema s = tuple(context, self, arg).getInternalSchema();
Ruby runtime = context.getRuntime();
try {
return new RubySchema(runtime, runtime.getClass("Schema"), new Schema(new Schema.FieldSchema("bag_0", s, DataType.BAG)));
} catch (FrontendException e) {
throw new RuntimeException("Error making map", e);
}
}
示例6: equals
import org.jruby.runtime.ThreadContext; //导入方法依赖的package包/类
/**
* Overrides equality leveraging DataByteArray's equality.
*
* @param context the context the method is being executed in
* @param arg a RubyDataByteArray against which to test equality
* @return true if they are equal, false otherwise
*/
@JRubyMethod(name = {"eql?", "=="})
public RubyBoolean equals(ThreadContext context, IRubyObject arg) {
Ruby runtime = context.getRuntime();
if (arg instanceof RubyDataByteArray) {
return RubyBoolean.newBoolean(runtime, internalDBA.equals(((RubyDataByteArray)arg).getDBA()));
} else {
return runtime.getFalse();
}
}
示例7: set
import org.jruby.runtime.ThreadContext; //导入方法依赖的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;
}
示例8: addInPlace
import org.jruby.runtime.ThreadContext; //导入方法依赖的package包/类
/**
* This method provides addition semantics, modifying the original Schema in place.
* This method can be given any number of arguments, much as with the constructor.
*
* @param context the context the method is being executed in
* @param args a varargs which can be any valid set of arguments that
* can initialize a RubySchema
*/
@JRubyMethod(name = "add!", rest = true)
public void addInPlace(ThreadContext context, IRubyObject[] args) {
Ruby runtime = context.getRuntime();
List<Schema.FieldSchema> lfs = internalSchema.getFields();
RubySchema rs = new RubySchema(runtime, runtime.getClass("Schema")).initialize(args);
for (Schema.FieldSchema fs : rs.getInternalSchema().getFields())
lfs.add(fs);
RubySchema.fixSchemaNames(internalSchema);
}
示例9: index
import org.jruby.runtime.ThreadContext; //导入方法依赖的package包/类
/**
* Given a field name, this will return the index of it in the schema.
*
* @param context the context the method is being executed in
* @param arg a field name to look for
* @return the index for that field name
*/
@JRubyMethod
public RubyFixnum index(ThreadContext context, IRubyObject arg) {
if (arg instanceof RubyString) {
try {
return new RubyFixnum(context.getRuntime(), internalSchema.getPosition(arg.toString()));
} catch (FrontendException e) {
throw new RuntimeException("Unable to find position for argument: " + arg);
}
} else {
throw new RuntimeException("Invalid arguement passed to index: " + arg);
}
}
示例10: map
import org.jruby.runtime.ThreadContext; //导入方法依赖的package包/类
/**
* This is a ruby method which takes an array of arguments and constructs a Map schema from them. The name
* will be set automatically.
*
* @param context the context the method is being executed in
* @param self the RubyClass for the Class object this was invoked on
* @param arg a list of arguments to instantiate the new RubySchema
* @return the new RubySchema
*/
@JRubyMethod(meta = true, name = {"m", "map"})
public static RubySchema map(ThreadContext context, IRubyObject self, IRubyObject arg) {
Schema s = tuple(context, self, arg).getInternalSchema();
Ruby runtime = context.getRuntime();
try {
return new RubySchema(runtime, runtime.getClass("Schema"), new Schema(new Schema.FieldSchema("map_0", s.getField(0).schema, DataType.MAP)));
} catch (FrontendException e) {
throw new RuntimeException("Error making map", e);
}
}
示例11: inspect
import org.jruby.runtime.ThreadContext; //导入方法依赖的package包/类
/**
* This method returns a string representation of the RubyDataBag. If given an optional
* argument, then if that argument is true, the contents of the bag will also be printed.
*
* @param context the context the method is being executed in
* @param args optional true/false argument passed to inspect
* @return string representation of the RubyDataBag
*/
@JRubyMethod(name = {"inspect", "to_s", "to_string"}, optional = 1)
public RubyString inspect(ThreadContext context, IRubyObject[] args) {
Ruby runtime = context.getRuntime();
StringBuilder sb = new StringBuilder();
sb.append("[DataBag: size: ").append(internalDB.size());
if (args.length > 0 && args[0].isTrue())
sb.append(" = ").append(internalDB.toString());
sb.append("]");
return RubyString.newString(runtime, sb);
}
示例12: clone
import org.jruby.runtime.ThreadContext; //导入方法依赖的package包/类
/**
* This method returns a copy of the encapsulated DataBag.
*
* @param context the context the method is being executed in
* @return the copied RubyDataBag
*/
//TODO see if a deepcopy is necessary as well (and consider adding to DataBag and Tuple)
@JRubyMethod
public RubyDataBag clone(ThreadContext context) {
DataBag b = mBagFactory.newDefaultBag();
for (Tuple t : this)
b.add(t);
Ruby runtime = context.getRuntime();
return new RubyDataBag(runtime, runtime.getClass("DataBag"), b);
}
示例13: RubyBindingsCollector
import org.jruby.runtime.ThreadContext; //导入方法依赖的package包/类
private RubyBindingsCollector(ThreadContext context) {
this.iterator = new CurrentBindingsIterator(context);
this.runtime = context.getRuntime();
}
示例14: clone
import org.jruby.runtime.ThreadContext; //导入方法依赖的package包/类
/**
* @param context the context the method is being executed in
* @return a RubySchema copy of the Schema
*/
@JRubyMethod
public RubySchema clone(ThreadContext context) {
Ruby runtime = context.getRuntime();
return new RubySchema(runtime, runtime.getClass("Schema"), internalSchema);
}
示例15: buildSelfString
import org.jruby.runtime.ThreadContext; //导入方法依赖的package包/类
/**
* This is a kind of pointless module class method, but is simple to understand.
* meta = true is waht makes this module class method
* The equivalent in ruby:
* module Foo
* def self.self_string
* return 'This is String is from Foo.self'
* end
* end
*
* @param context ThreadContext
* @param recv the receiver
* @return A RubyString.
*/
@JRubyMethod(name = "self_string", module = true, meta = true)
public static IRubyObject buildSelfString(ThreadContext context, IRubyObject recv) {
Ruby runtime = context.getRuntime();
return runtime.newString("This is String is from Foo.self");
}