예제 #1
0
 // @RubyLevelMethod(name="each_index")
 public RubyValue each_index(RubyBlock block) {
   for (int i = 0; i < size(); i++) {
     RubyValue v = block.invoke(this, new RubyFixnum(i));
     if (block.breakedOrReturned()) {
       return v;
     }
   }
   return this;
 }
예제 #2
0
 // @RubyLevelMethod(name="reverse_each")
 public RubyValue reverse_each(RubyBlock block) {
   ListIterator /*<RubyValue>*/ ite = array_.listIterator(array_.size());
   while (ite.hasPrevious()) {
     RubyValue v = block.invoke(this, (RubyValue) ite.previous());
     if (block.breakedOrReturned()) {
       return v;
     }
   }
   return this;
 }
예제 #3
0
 // @RubyLevelMethod(name="each")
 public RubyValue each(RubyBlock block) {
   //        for (RubyValue item : array_) {
   for (Iterator iter = array_.iterator(); iter.hasNext(); ) {
     RubyValue item = (RubyValue) iter.next();
     RubyValue v = block.invoke(this, item);
     if (block.breakedOrReturned()) {
       return v;
     }
   }
   return this;
 }
예제 #4
0
 // @RubyLevelMethod(name="delete_if")
 public RubyValue delete_if(RubyBlock block) {
   for (int i = 0; i < array_.size(); ) {
     RubyValue r = block.invoke(this, (RubyValue) array_.get(i));
     if (r.isTrue()) {
       array_.remove(i);
     } else {
       ++i;
     }
   }
   return this;
 }
예제 #5
0
  // @RubyLevelMethod(name="delete")
  public RubyValue delete(RubyValue item, RubyBlock block) {
    boolean found = false;
    while (array_.remove(item)) {
      found = true;
    }

    if (block != null && !found) {
      return block.invoke(item);
    } else {
      return found ? item : RubyConstant.QNIL;
    }
  }
예제 #6
0
 // @RubyLevelMethod(name="new", singleton=true)
 public static RubyValue newArray(RubyValue receiver, RubyArray args, RubyBlock block) {
   RubyArray a;
   if (null == args) {
     a = new RubyArray();
   } else if (null == block) {
     if (args.get(0) instanceof RubyArray) {
       a = (RubyArray) args.get(0).clone();
     } else {
       RubyFixnum size = (RubyFixnum) args.get(0);
       RubyValue default_value = args.get(1);
       a = ObjectFactory.createArray(size.toInt(), default_value);
     }
   } else {
     RubyFixnum size = (RubyFixnum) args.get(0);
     a = new RubyArray();
     for (int i = 0; i < size.toFloat(); i++) {
       RubyValue return_value = block.invoke(receiver, ObjectFactory.createFixnum(i));
       a.add(return_value);
     }
   }
   a.setRubyClass((RubyClass) receiver);
   return a;
 }