Example #1
0
  private RubyValue to_i(int radix) {
    String value = toString();

    value = StringMe.replaceAll(value, "[^+\\-a-zA-Z0-9]", "");
    int end = value.indexOf('+', 1);
    int end1 = value.indexOf('-', 1);

    if (end < 0) {
      if (end1 < 0) {
        end = value.length();
      } else {
        end = end1;
      }
    } else {
      if (end1 >= 0) {
        end = Math.min(end, end1);
      }
    }

    value = value.substring(0, end);
    int nPoint = value.indexOf('.');
    if (nPoint >= 0) value = value.substring(0, nPoint);

    if (radix >= 2 && radix <= 36) {
      HugeInt bigint;
      try {
        bigint = new HugeInt(value, radix);
      } catch (NumberFormatException e) {
        return ObjectFactory.FIXNUM0;
      }
      return RubyBignum.bignorm(bigint);
    }
    throw new RubyException(RubyRuntime.ArgumentErrorClass, "illegal radix " + radix);
  }
  @RubyLevelMethod(name = "sleep", module = true)
  public static RubyValue sleep(RubyValue receiver, RubyValue arg) {
    long milliseconds = RubyTypesUtil.convertToJavaLong(arg) * 1000;
    long startTime = System.currentTimeMillis();

    RubyThread.sleep(milliseconds);

    long endTime = System.currentTimeMillis();
    return ObjectFactory.createFixnum((int) Math.round((endTime - startTime) / 1000.0));
  }
Example #3
0
  @RubyLevelMethod(name = "**")
  public RubyValue pow(RubyValue v) {
    if (v instanceof RubyFixnum) {
      int p = ((RubyFixnum) v).value_;
      if (p == 0) {
        return ObjectFactory.FIXNUM1;
      } else if (p == 1) {
        return this;
      }

      if (p > 0) {
        BigInteger b = BigInteger.valueOf(this.value_);
        return RubyBignum.bignorm(b.pow(p));
      }

      return ObjectFactory.createFloat(Math.pow(this.value_, p));
    } else if (v instanceof RubyFloat) {
      return ObjectFactory.createFloat(Math.pow(this.value_, v.toFloat()));
    }

    return coerceBin(RubyID.powID, v);
  }