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
 // @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);
   }
 }