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);
  }
Example #2
0
  private RubyString gsub(RubyString g, RubyArray args) {
    if (null == args || args.size() != 2) {
      int actual_argc = (null == args) ? 0 : args.size();
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "in `gsub': wrong number of arguments (" + actual_argc + " for 2)");
    }

    if (!(args.get(1) instanceof RubyString)) {
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "can't convert " + args.get(1).getRubyClass().getName() + " into String");
    }

    RubyString s = (RubyString) args.get(1);

    if (args.get(0) instanceof RubyRegexp) {
      RubyRegexp r = (RubyRegexp) args.get(0);
      return r.gsub(g, s);
    } else if (args.get(0) instanceof RubyString) {
      RubyString r = (RubyString) args.get(0);
      String result = StringMe.replaceAll(g.toString(), r.toString(), s.toString());
      return ObjectFactory.createString(result);
    } else {
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "wrong argument type " + args.get(0).getRubyClass().getName() + " (expected Regexp)");
    }
  }