当前位置: 首页>>代码示例>>Java>>正文


Java JRubyMethod类代码示例

本文整理汇总了Java中org.jruby.anno.JRubyMethod的典型用法代码示例。如果您正苦于以下问题:Java JRubyMethod类的具体用法?Java JRubyMethod怎么用?Java JRubyMethod使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


JRubyMethod类属于org.jruby.anno包,在下文中一共展示了JRubyMethod类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: fuzz

import org.jruby.anno.JRubyMethod; //导入依赖的package包/类
@JRubyMethod(name = "fuzz", required = 1, module = true)
public static IRubyObject fuzz(IRubyObject self, IRubyObject length) {
    final Random rnd = new Random();
    final long llen = length.convertToInteger().getLongValue();
    final int len = (int) llen;
    final byte[] bytes = new byte[len];
    for (int i = 0; i < len; i++) {
        switch (rnd.nextInt(4)) {
            case 0:
                bytes[i] = '\'';
                break;
            case 1:
                bytes[i] = '\"';
                break;
            case 2:
                bytes[i] = '\\';
                break;
            case 3:
                bytes[i] = ' ';
                break;
        }
    }
    return RubyString.newString(self.getRuntime(), bytes);
}
 
开发者ID:airbnb,项目名称:collapstring,代码行数:25,代码来源:CollapstringService.java

示例2: set

import org.jruby.anno.JRubyMethod; //导入依赖的package包/类
@JRubyMethod(name = "set", required=2)
public IRubyObject set(ThreadContext context, IRubyObject i, IRubyObject val) {
    int j = RubyNumeric.num2int(i);
    if (j >=0 && j < cnt) {
        if (j >= tailoff()) {
            RubyArray newTail = tail.aryDup();
            newTail.store(j & 0x01f, val);
            return new PersistentVector(context.runtime, getMetaClass()).initialize(context, cnt, shift, root, newTail);
        }
        return new PersistentVector(context.runtime, getMetaClass()).initialize(context, cnt, shift, doSet(context, shift, root, j, val), tail);
    }

    if (j == cnt)
        add(context, val);

    throw new IndexOutOfBoundsException();
}
 
开发者ID:Who828,项目名称:persistent_data_structures,代码行数:18,代码来源:PersistentVectorLibrary.java

示例3: add

import org.jruby.anno.JRubyMethod; //导入依赖的package包/类
@JRubyMethod(name = {"add", "append"}, required = 1)
public IRubyObject add(ThreadContext context, IRubyObject val) {
    if (cnt - tailoff() < 32) {
        PersistentVector ret = new PersistentVector(context.runtime, getMetaClass());
        RubyArray newTail = tail.aryDup();
        newTail.append(val);
        return ret.initialize(context, this.cnt+1, this.shift, this.root, newTail);
    }

    Node newroot;
    Node tailnode = new Node(context.runtime, Node).initialize_params_arry(context, root.edit, tail);
    int newshift = shift;

    if ((cnt >>> 5) > (1 << shift)) {
        newroot = new Node(context.runtime, Node).initialize_params(context, root.edit);
        newroot.array.store(0, root);
        newroot.array.store(1, newPath(context, root.edit, shift, tailnode));
        newshift += 5;
    } else
        newroot = pushTail(context, shift, root, tailnode);

    RubyArray arry = RubyArray.newArray(context.runtime);
    arry.store(0, val);

    return new PersistentVector(context.runtime, getMetaClass()).initialize(context, cnt + 1, newshift, newroot, arry);
}
 
开发者ID:Who828,项目名称:persistent_data_structures,代码行数:27,代码来源:PersistentVectorLibrary.java

示例4: eql

import org.jruby.anno.JRubyMethod; //导入依赖的package包/类
@JRubyMethod(name = "eql?")
public IRubyObject eql(ThreadContext context, IRubyObject obj) {
    Ruby runtime = context.runtime;

    if (this == obj) {
        return runtime.getTrue();
    }

    if (obj instanceof PersistentVector) {
        Node otherRoot = ((PersistentVector)obj).root;
        RubyArray otherTail = ((PersistentVector)obj).tail;
        if (root.equals(otherRoot) && tail.equals(otherTail)) {
            return runtime.getTrue();
        }
    }
    return runtime.getFalse();
}
 
开发者ID:Who828,项目名称:persistent_data_structures,代码行数:18,代码来源:PersistentVectorLibrary.java

示例5: compare

import org.jruby.anno.JRubyMethod; //导入依赖的package包/类
/**
 * This calls to the static method compare of DataByteArray. Given two DataByteArrays, it will call it
 * on the underlying bytes.
 *
 * @param context the context the method is being executed in
 * @param self    an class which contains metadata on the calling class (required for static Ruby methods)
 * @param arg1    a RubyDataByteArray or byte array to compare
 * @param arg2    a RubyDataByteArray or byte array to compare
 * @return        the Fixnum result of comparing arg1 and arg2's bytes
 */
@JRubyMethod
public static RubyFixnum compare(ThreadContext context, IRubyObject self, IRubyObject arg1, IRubyObject arg2) {
    byte[] buf1, buf2;
    if (arg1 instanceof RubyDataByteArray) {
        buf1 = ((RubyDataByteArray)arg1).getDBA().get();
    } else {
        buf1 = (byte[])arg1.toJava(byte[].class);
    }
    if (arg2 instanceof RubyDataByteArray) {
        buf2 = ((RubyDataByteArray)arg2).getDBA().get();
    } else {
        buf2 = (byte[])arg2.toJava(byte[].class);
    }
    return RubyFixnum.newFixnum(context.getRuntime(), DataByteArray.compare(buf1, buf2));
}
 
开发者ID:sigmoidanalytics,项目名称:spork,代码行数:26,代码来源:RubyDataByteArray.java

示例6: setName

import org.jruby.anno.JRubyMethod; //导入依赖的package包/类
/**
 * This method allows the user to set the name of the alias of the FieldSchema of the encapsulated
 * Schema. This method only works if the Schema has one FieldSchema.
 *
 * @param arg a RubyString to set the name to
 * @return    the new name
 */
@JRubyMethod(name = "name=")
public RubyString setName(IRubyObject arg) {
    if (arg instanceof RubyString) {
         if (internalSchema.size() != 1)
             throw new RuntimeException("Can only set name if there is one schema present");
         try {
             internalSchema.getField(0).alias = arg.toString();
             return (RubyString)arg;
         } catch (FrontendException e) {
             throw new RuntimeException("Unable to get field from Schema", e);
         }
    } else {
         throw new RuntimeException("Improper argument passed to 'name=':" + arg);
    }
}
 
开发者ID:sigmoidanalytics,项目名称:spork-streaming,代码行数:23,代码来源:RubySchema.java

示例7: each

import org.jruby.anno.JRubyMethod; //导入依赖的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;
}
 
开发者ID:sigmoidanalytics,项目名称:spork-streaming,代码行数:26,代码来源:RubyDataBag.java

示例8: flatten

import org.jruby.anno.JRubyMethod; //导入依赖的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;
}
 
开发者ID:sigmoidanalytics,项目名称:spork-streaming,代码行数:25,代码来源:RubyDataBag.java

示例9: add

import org.jruby.anno.JRubyMethod; //导入依赖的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);
}
 
开发者ID:jruby,项目名称:jruby-examples,代码行数:19,代码来源:RubyFoo.java

示例10: mult

import org.jruby.anno.JRubyMethod; //导入依赖的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);
}
 
开发者ID:jruby,项目名称:jruby-examples,代码行数:17,代码来源:RubyBar.java

示例11: initialize

import org.jruby.anno.JRubyMethod; //导入依赖的package包/类
@JRubyMethod(required = 1, optional = 2)
public IRubyObject initialize(ThreadContext context, IRubyObject[] args) {
    Ruby runtime = context.runtime;
    this.storage = runtime.newArray();
    IRubyObject ary = null;
    if (!(args[0] instanceof RubySymbol)) {
        throw runtime.newArgumentError("Expected Symbol for type name");
    }
    this.fieldType = Utils.rubyToFieldType(args[0]);
    if (fieldType == Descriptors.FieldDescriptor.Type.MESSAGE
            || fieldType == Descriptors.FieldDescriptor.Type.ENUM) {
        if (args.length < 2)
            throw runtime.newArgumentError("Expected at least 2 arguments for message/enum");
        typeClass = args[1];
        if (args.length > 2)
            ary = args[2];
        Utils.validateTypeClass(context, fieldType, typeClass);
    } else {
        if (args.length > 2)
            throw runtime.newArgumentError("Too many arguments: expected 1 or 2");
        if (args.length > 1)
            ary = args[1];
    }
    if (ary != null) {
        RubyArray arr = ary.convertToArray();
        for (int i = 0; i < arr.size(); i++) {
            this.storage.add(arr.eltInternal(i));
        }
    }
    return this;
}
 
开发者ID:bazelbuild,项目名称:bazel,代码行数:32,代码来源:RubyRepeatedField.java

示例12: set

import org.jruby.anno.JRubyMethod; //导入依赖的package包/类
/**
 * This method calls the set method of the underlying DataByteArray with one exception:
 * if given a RubyDataByteArray, it will set the encapsulated DataByteArray to be equal.
 *
 * @param arg an object to set the encapsulated DataByteArray's bits to. In the case of
 *            a RubyString or byte array, the underlying bytes will be sit directly. In
 *            the case of a RubyDataByteArray, the encapsulated DataByteArray will be set
 *            equal to arg.
 */
@JRubyMethod
public void set(IRubyObject arg) {
    if (arg instanceof RubyDataByteArray) {
        internalDBA = ((RubyDataByteArray)arg).getDBA();
    } else if (arg instanceof RubyString) {
        internalDBA.set(arg.toString());
    } else {
        internalDBA.set((byte[])arg.toJava(byte[].class));
    }
}
 
开发者ID:sigmoidanalytics,项目名称:spork,代码行数:20,代码来源:RubyDataByteArray.java

示例13: oneof

import org.jruby.anno.JRubyMethod; //导入依赖的package包/类
@JRubyMethod
public IRubyObject oneof(ThreadContext context, IRubyObject name, Block block) {
    RubyOneofDescriptor oneofdef = (RubyOneofDescriptor)
            cOneofDescriptor.newInstance(context, Block.NULL_BLOCK);
    RubyOneofBuilderContext ctx = (RubyOneofBuilderContext)
            cOneofBuilderContext.newInstance(context, oneofdef, Block.NULL_BLOCK);
    oneofdef.setName(context, name);
    Binding binding = block.getBinding();
    binding.setSelf(ctx);
    block.yieldSpecific(context);
    descriptor.addOneof(context, oneofdef);
    return context.runtime.getNil();
}
 
开发者ID:bazelbuild,项目名称:bazel,代码行数:14,代码来源:RubyMessageBuilderContext.java

示例14: finalizeToPool

import org.jruby.anno.JRubyMethod; //导入依赖的package包/类
@JRubyMethod(name = "finalize_to_pool")
public IRubyObject finalizeToPool(ThreadContext context, IRubyObject rbPool) {
    RubyDescriptorPool pool = (RubyDescriptorPool) rbPool;
    for (int i = 0; i < this.pendingList.size(); i++) {
        IRubyObject defRb = this.pendingList.entry(i);
        if (defRb instanceof RubyDescriptor) {
            pool.addToSymtab(context, (RubyDescriptor) defRb);
        } else {
            pool.addToSymtab(context, (RubyEnumDescriptor) defRb);
        }
    }
    this.pendingList = context.runtime.newArray();
    return context.runtime.getNil();
}
 
开发者ID:bazelbuild,项目名称:bazel,代码行数:15,代码来源:RubyBuilder.java

示例15: each

import org.jruby.anno.JRubyMethod; //导入依赖的package包/类
@JRubyMethod
public IRubyObject each(ThreadContext context, Block block) {
    Ruby runtime = context.runtime;
    for (Descriptors.EnumValueDescriptor enumValueDescriptor : descriptor.getValues()) {
        block.yield(context, runtime.newArray(runtime.newSymbol(enumValueDescriptor.getName()),
                runtime.newFixnum(enumValueDescriptor.getNumber())));
    }
    return runtime.getNil();
}
 
开发者ID:bazelbuild,项目名称:bazel,代码行数:10,代码来源:RubyEnumDescriptor.java


注:本文中的org.jruby.anno.JRubyMethod类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。