Ejemplo n.º 1
0
  private RubyString sub(RubyString g, RubyArray args) {
    if (null == args || args.size() != 2) {
      int actual_argc = (null == args) ? 0 : args.size();
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "in `sub': 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.sub(g, s);
    } else if (args.get(0) instanceof RubyString) {
      RubyString r = (RubyString) args.get(0);
      String result = StringMe.replaceFirst(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)");
    }
  }
Ejemplo n.º 2
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);
  }
Ejemplo n.º 3
0
 // @RubyLevelMethod(name="%")
 public RubyValue format(RubyValue arg) {
   String format = toString();
   String s;
   // RHO_MOBILE
   if (arg instanceof RubyArray) {
     // s = StringMe.format(format,
     // com.xruby.runtime.lang.RubyKernelModule.buildFormatArg((RubyArray)arg, 0));
     s = StringMe.format(format, (RubyArray) arg);
   } else {
     // s = StringMe.format(format, com.xruby.runtime.lang.RubyKernelModule.buildFormatArg(new
     // RubyArray(arg), 0));
     RubyArray args = new RubyArray(arg);
     s = StringMe.format(format, args);
   }
   return ObjectFactory.createString(s);
 }