Exemplo n.º 1
0
  // @RubyLevelMethod(name="[]", alias="slice")
  public RubyValue aref(RubyValue arg) {
    if (arg instanceof RubyFixnum) {
      return this.get(arg.toInt());
    }

    if (arg instanceof RubySymbol) {
      throw new RubyException(RubyRuntime.TypeErrorClass, "Symbol as array index");
    }

    if (arg instanceof RubyRange) {
      RubyRange range = (RubyRange) arg;
      int begin = range.getLeft().toInt();
      int end = range.getRight().toInt();
      if (begin < 0) {
        begin = this.size() + begin;
      }
      if (end < 0) {
        end = this.size() + end;
      }

      if (!range.isExcludeEnd()) {
        ++end;
      }

      RubyArray resultValue = this.subarray(begin, end - begin);
      if (null == resultValue) return RubyConstant.QNIL;
      return resultValue;
      // return (null == resultValue ? RubyConstant.QNIL : resultValue);
    }

    return this.get(arg.toInt());
  }
Exemplo n.º 2
0
  // RHO_COMMENT
  public boolean equals(Object o, boolean bConvToAry) {
    if (this == o) {
      return true;
    }

    if (o instanceof RubyArray) {
      RubyArray that = (RubyArray) o;
      int size = array_.size();
      if (size != that.size()) {
        return false;
      }

      for (int i = 0; i < size; ++i) {
        if (!this.get(i).equals(that.get(i))) {
          return false;
        }
      }

      return true;
    } else if (o instanceof RubyValue && bConvToAry) {
      RubyValue v = (RubyValue) o;
      if (!v.respondTo(RubyID.toAryID)) {
        return false;
      } else {
        return v.equals(this);
      }
    } else {
      return false;
    }
  }
Exemplo n.º 3
0
 public int hashCode() {
   int hash = 0;
   //        for (RubyValue v : array_) {
   for (Iterator iter = array_.iterator(); iter.hasNext(); ) {
     RubyValue v = (RubyValue) iter.next();
     hash += v.hashCode();
   }
   return hash;
 }
Exemplo n.º 4
0
  // @RubyLevelMethod(name="[]", alias="slice")
  public RubyValue aref(RubyValue begin, RubyValue length) {
    if (begin instanceof RubySymbol) {
      throw new RubyException(RubyRuntime.TypeErrorClass, "Symbol as array index");
    }

    RubyArray resultValue = this.subarray(begin.toInt(), length.toInt());
    if (null == resultValue) return RubyConstant.QNIL;
    return resultValue;
    //        return (null == resultValue ? RubyConstant.QNIL : resultValue);
  }
Exemplo n.º 5
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;
 }
Exemplo n.º 6
0
  // @RubyLevelMethod(name="last")
  public RubyValue last(RubyValue v) {
    int n = v.toInt();
    int size = this.array_.size();
    if (n > size) {
      n = size;
    }

    return new RubyArray(this.array_.subList(size - n, size));
  }
Exemplo n.º 7
0
  // @RubyLevelMethod(name="&")
  public RubyArray and(RubyValue value) {
    RubyArray other = value.toAry();

    RubyArray a = new RubyArray();
    //        for (RubyValue v : array_) {
    for (Iterator iter = array_.iterator(); iter.hasNext(); ) {
      RubyValue v = (RubyValue) iter.next();
      if (other.include(v) == RubyConstant.QTRUE && a.include(v) == RubyConstant.QFALSE) {
        a.add(v);
      }
    }
    return a;
  }
Exemplo n.º 8
0
 // @RubyLevelMethod(name="[]=")
 public RubyValue aset(RubyValue arg0, RubyValue arg1) {
   if (arg0 instanceof RubyRange) {
     RubyRange range = (RubyRange) arg0;
     int left = range.getLeft().toInt();
     int right = range.getRight().toInt();
     int l_index = getRealIndex(size(), left);
     int r_index = getRealIndex(size(), right);
     int length = r_index - l_index + 1;
     return replace(l_index, length, arg1);
   } else {
     return set(arg0.toInt(), arg1);
   }
 }
Exemplo n.º 9
0
  // @RubyLevelMethod(name="assoc")
  public RubyValue assoc(RubyValue arg) {
    RubyValue val = null;
    for (int i = 0; i < size(); i++) {
      val = get(i);
      if (val instanceof RubyArray) {
        if (((RubyArray) val).size() > 0) {
          RubyValue tmp = ((RubyArray) val).get(0);
          if (arg.equals(tmp)) {
            return val;
          }
        }
      }
    }

    return RubyConstant.QNIL;
  }
Exemplo n.º 10
0
  // @RubyLevelMethod(name="rassoc")
  public RubyValue rassoc(RubyValue arg) {
    int size = this.array_.size();
    for (int i = 0; i < size; i++) {
      RubyValue val = get(i);
      if (val instanceof RubyArray) {
        if (((RubyArray) val).size() > 1) {
          RubyValue tmp = ((RubyArray) val).get(1);
          if (arg.equals(tmp)) {
            return val;
          }
        }
      }
    }

    return RubyConstant.QNIL;
  }
Exemplo n.º 11
0
 // @RubyLevelMethod(name="-")
 public RubyArray minus(RubyValue v) {
   return this.minus(v.toAry());
 }
Exemplo n.º 12
0
 // @RubyLevelMethod(name="+")
 public RubyArray plus(RubyValue v) {
   return this.plus(v.toAry());
 }
Exemplo n.º 13
0
 // @RubyLevelMethod(name="concat")
 public RubyArray concat(RubyValue v) {
   RubyArray ary = v.toAry();
   array_.addAll(ary.array_);
   return this;
 }
Exemplo n.º 14
0
 // @RubyLevelMethod(name="<=>")
 public RubyValue compare(RubyValue v) {
   return this.compare(v.toAry());
 }
Exemplo n.º 15
0
 // @RubyLevelMethod(name="delete_at")
 public RubyValue deleteAt(RubyValue v) {
   return this.delete_at(v.toInt());
 }
Exemplo n.º 16
0
 // @RubyLevelMethod(name="at")
 public RubyValue at(RubyValue value) {
   return this.get(value.toInt());
 }