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)"); } }