Ejemplo n.º 1
0
  // @RubyLevelMethod(name="sub!")
  public RubyValue sub_danger(RubyArray args, RubyBlock block) {
    if (null == block) {
      RubyString result = sub(this, args);
      if (result == this) {
        return RubyConstant.QNIL;
      } else {
        return setString(result.toString());
      }
    } else {
      if (null == args || args.size() != 1) {
        int actual_argc = (null == args) ? 0 : args.size();
        throw new RubyException(
            RubyRuntime.ArgumentErrorClass,
            "in `sub!': wrong number of arguments (" + actual_argc + " for 1)");
      }
      if (!(args.get(0) instanceof RubyRegexp)) {
        throw new RubyException(
            RubyRuntime.ArgumentErrorClass,
            "wrong argument type " + args.get(0).getRubyClass().getName() + " (expected Regexp)");
      }

      RubyRegexp r = (RubyRegexp) args.get(0);
      return setString(r.sub(this, block).toString());
    }
  }
Ejemplo n.º 2
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.º 3
0
  // @RubyLevelMethod(name="sub")
  public RubyValue sub(RubyArray args, RubyBlock block) {
    if (null == block) {
      return sub(this, args);
    } else {
      if (null == args || args.size() != 1) {
        int actual_argc = (null == args) ? 0 : args.size();
        throw new RubyException(
            RubyRuntime.ArgumentErrorClass,
            "in `sub': wrong number of arguments (" + actual_argc + " for 1)");
      }
      if (!(args.get(0) instanceof RubyRegexp)) {
        throw new RubyException(
            RubyRuntime.ArgumentErrorClass,
            "wrong argument type " + args.get(0).getRubyClass().getName() + " (expected Regexp)");
      }

      RubyRegexp r = (RubyRegexp) args.get(0);
      return r.sub(this, block);
    }
  }