Example #1
0
 public RubyValue replace(int start, int length, RubyValue value) {
   int index = getRealIndex(start);
   if (value == RubyConstant.QNIL) {
     for (int i = 0; i < length; i++) {
       array_.remove(index);
     }
     return value;
   }
   if (length < 0) {
     throw new RubyException(RubyRuntime.IndexErrorClass, "negative length (" + length + ")");
   } else if (0 == length) {
     if (value instanceof RubyArray) {
       array_.addAll(index, ((RubyArray) value).array_);
     } else {
       array_.add(index, value);
     }
   } else {
     for (int i = 0; i < length - 1; ++i) {
       array_.remove(index);
     }
     if (value instanceof RubyArray) {
       array_.remove(index);
       array_.addAll(index, ((RubyArray) value).array_);
     } else {
       array_.set(index, value);
     }
   }
   return value;
 }
Example #2
0
 private boolean remove(RubyValue v) {
   boolean r = false;
   while (array_.remove(v)) {
     r = true;
   }
   return r;
 }
Example #3
0
  private RubyArray delete_at(int begin, int length) {
    final int arraySize = array_.size();
    if (begin > arraySize) {
      return null;
    }

    if (length < 0) {
      return null;
    }

    if (begin < 0) {
      begin += arraySize;
    }

    if (begin + length > arraySize) {
      length = arraySize - begin;
      if (length < 0) {
        length = 0;
      }
    }

    while (length > 0) {
      array_.remove(begin);
      length--;
    }

    return this;
  }
Example #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;
 }
Example #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;
    }
  }
Example #6
0
  public RubyValue delete_at(int index) {
    if (index >= size()) {
      return RubyConstant.QNIL;
    }

    if (index < 0) {
      index += size();
    }

    if (index < 0) {
      return RubyConstant.QNIL;
    }

    return (RubyValue) array_.remove(index);
  }
Example #7
0
  // @RubyLevelMethod(name="uniq!")
  public RubyValue uniq_danger() {
    boolean b = false;
    for (int i = 0; i < array_.size(); ++i) {
      for (int j = i + 1; j < array_.size(); ) {
        if (RubyAPI.testEqual((RubyValue) array_.get(i), (RubyValue) array_.get(j))) {
          array_.remove(j);
          b = true;
        } else {
          ++j;
        }
      }
    }
    if (!b) return RubyConstant.QNIL;

    return this;
    // return b ? this : RubyConstant.QNIL;
  }