示例#1
0
 // @RubyLevelMethod(name="indexes", alias="indices")
 public RubyValue indexes(RubyArray args) {
   RubyArray a = new RubyArray(args.size());
   for (int i = 0; i < size(); i++) {
     RubyFixnum n = (RubyFixnum) args.get(i);
     a.add(get(n.toInt()));
   }
   return a;
 }
示例#2
0
  // @RubyLevelMethod(name="*")
  public RubyValue run(RubyValue arg, RubyBlock block) {
    if (arg instanceof RubyFixnum) {
      RubyFixnum times = (RubyFixnum) arg;
      return times(times.toInt());
    } else if (arg instanceof RubyString) {
      return RubyAPI.callOneArgMethod(this, arg, block, RubyID.joinID);
    }

    throw new RubyException(
        RubyRuntime.TypeErrorClass, "no implicit conversion from " + arg + " to integer");
  }
示例#3
0
  // @RubyLevelMethod(name="slice!")
  public RubyValue slice_danger(RubyArray args) {
    if (1 == args.size()) {
      Object argValue = args.get(0);
      if (argValue instanceof RubyFixnum) {
        RubyFixnum index = (RubyFixnum) argValue;
        return delete_at(index.toInt());
      } else if (args.get(0) instanceof RubyRange) {
        int begin = ((RubyFixnum) ((RubyRange) args.get(0)).getLeft()).toInt();
        int end = ((RubyFixnum) ((RubyRange) args.get(0)).getRight()).toInt();
        if (begin < 0) {
          begin = size() + begin;
        }
        if (end < 0) {
          end = size() + end;
        }

        if (!((RubyRange) args.get(0)).isExcludeEnd()) {
          ++end;
        }

        RubyArray resultValue = delete_at(begin, end - begin);
        if (null == resultValue) return RubyConstant.QNIL;
        return resultValue;
        //                return (null == resultValue ? RubyConstant.QNIL : resultValue);
      }
    } else if (2 == args.size()) {
      Object arg0Value = args.get(0);
      Object arg1Value = args.get(1);
      if (arg0Value instanceof RubyFixnum && arg1Value instanceof RubyFixnum) {
        int begin = ((RubyFixnum) arg0Value).toInt();
        int length = ((RubyFixnum) arg1Value).toInt();
        RubyArray resultValue = delete_at(begin, length);
        if (null == resultValue) return RubyConstant.QNIL;
        return resultValue;
        //                return (null == resultValue ? RubyConstant.QNIL : resultValue);
      }
    }

    // TODO
    throw new RubyException("not implemented");
  }
示例#4
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;
 }