Package org.jruby.runtime

Examples of org.jruby.runtime.JavaInternalBlockBody


    public static IRubyObject count(ThreadContext context, IRubyObject self, final Block block) {
        final Ruby runtime = context.runtime;
        final int result[] = new int[] { 0 };
       
        if (block.isGiven()) {
            each(context, self, new JavaInternalBlockBody(context, "Enumerable#count", block.arity()) {
                public IRubyObject yield(ThreadContext context, IRubyObject arg) {
                    if (block.yield(context, arg).isTrue()) result[0]++;
                    return runtime.getNil();
                }
            });
        } else {
            if (self.respondsTo("size")) return self.callMethod(context, "size");
           
            each(context, self, new JavaInternalBlockBody(context, "Enumerable#count", Arity.NO_ARGUMENTS) {
                public IRubyObject yield(ThreadContext context, IRubyObject unusedValue) {
                    result[0]++;
                    return runtime.getNil();
                }
            });
View Full Code Here


        final Ruby runtime = context.runtime;
        final int result[] = new int[] { 0 };
       
        if (block.isGiven()) runtime.getWarnings().warn(ID.BLOCK_UNUSED , "given block not used");
       
        each(context, self, new JavaInternalBlockBody(context, "Enumerable#count", Arity.ONE_REQUIRED) {
            public IRubyObject yield(ThreadContext context, IRubyObject blockArg) {
                if (blockArg.equals(methodArg)) result[0]++;
               
                return runtime.getNil();
            }
View Full Code Here

     */
    private static IRubyObject cycleCommon(ThreadContext context, IRubyObject self, long nv, final Block block) {
        final Ruby runtime = context.runtime;
        final RubyArray result = runtime.newArray();

        each(context, self, new JavaInternalBlockBody(block.arity()) {
            public IRubyObject yield(ThreadContext context, IRubyObject arg) {
                synchronized (result) { result.append(arg); }
                block.yield(context, arg);
                return runtime.getNil();           
            }
View Full Code Here

        if (len == 0) return runtime.newEmptyArray();

        final RubyArray result = runtime.newArray();

        try {
            each(context, self, new JavaInternalBlockBody(Arity.ONE_REQUIRED) {
                long i = len; // Atomic ?
                public IRubyObject yield(ThreadContext context, IRubyObject arg) {
                    synchronized (result) {
                        result.append(arg);
                        if (--i == 0) throw JumpException.SPECIAL_JUMP;
View Full Code Here

        final Ruby runtime = context.runtime;
        final RubyArray result = runtime.newArray();

        try {
            each(context, self, new JavaInternalBlockBody(block.arity()) {
                public IRubyObject yield(ThreadContext context, IRubyObject arg) {
                    if (!block.yield(context, arg).isTrue()) throw JumpException.SPECIAL_JUMP;
                    synchronized (result) { result.append(arg); }
                    return runtime.getNil();
                }
View Full Code Here

        if (len < 0) throw runtime.newArgumentError("attempt to drop negative size");

        final RubyArray result = runtime.newArray();

        try {
            each(context, self, new JavaInternalBlockBody(Arity.NO_ARGUMENTS) {
                long i = len; // Atomic ?
                public IRubyObject yield(ThreadContext context, IRubyObject arg) {
                    synchronized (result) {
                        if (i == 0) {
                            // While iterating over an RubyEnumerator, "arg"
View Full Code Here

        final Ruby runtime = context.runtime;
        final RubyArray result = runtime.newArray();

        try {
            each(context, self, new JavaInternalBlockBody(context, "Enumerable#drop_while", block.arity()) {
                boolean memo = false;
                public IRubyObject yield(ThreadContext context, IRubyObject arg) {
                    if (!memo && !block.yield(context, arg).isTrue()) memo = true;
                    if (memo) synchronized (result) { result.append(arg); }
                    return runtime.getNil();
View Full Code Here

    @JRubyMethod(name = "first")
    public static IRubyObject first(ThreadContext context, IRubyObject self) {
        final IRubyObject[] holder = new IRubyObject[]{ context.runtime.getNil() };

        try {
            each(context, self, new JavaInternalBlockBody(context, null, Arity.ONE_REQUIRED) {
                public IRubyObject yield(ThreadContext context, IRubyObject arg) {
                    holder[0] = arg;
                    throw JumpException.SPECIAL_JUMP;
                }
            });
View Full Code Here

        if (firstCount < 0) throw runtime.newArgumentError("negative index");
        if (firstCount == 0) return result;

        try {
            each(context, self, new JavaInternalBlockBody(context, null, Arity.ONE_REQUIRED) {
                private int iter = RubyNumeric.fix2int(num);               
                public IRubyObject yield(ThreadContext context, IRubyObject arg) {
                    result.append(arg);
                    if (iter-- == 1) throw JumpException.SPECIAL_JUMP;
                    return runtime.getNil();               
View Full Code Here

        if (self instanceof RubyArray) {
            RubyArray selfArray = (RubyArray) self;
            final IRubyObject[][] valuesAndCriteria = new IRubyObject[selfArray.size()][2];

            each(context, self, new JavaInternalBlockBody(Arity.OPTIONAL) {
                AtomicInteger i = new AtomicInteger(0);
                public IRubyObject yield(ThreadContext context, IRubyObject arg) {
                    IRubyObject[] myVandC = valuesAndCriteria[i.getAndIncrement()];
                    myVandC[0] = arg;
                    myVandC[1] = block.yield(context, arg);
View Full Code Here

TOP

Related Classes of org.jruby.runtime.JavaInternalBlockBody

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.