Exemplo n.º 1
0
  // @RubyLevelMethod(name="split")
  public RubyValue split(RubyArray args) {
    RubyValue r = (null == args) ? GlobalVariables.get("$;") : args.get(0);

    Collection /*<String>*/ splitResult;
    boolean bSkipFirstEmptyItem = false;
    if (r == RubyConstant.QNIL) {
      splitResult = split(this, " ");
    } else if (r instanceof RubyRegexp) {
      RubyRegexp reg = (RubyRegexp) r;
      splitResult = split(this, reg, args);

      if (reg.getPattern().getPattern().startsWith("(?=")) bSkipFirstEmptyItem = true;
    } else if (r instanceof RubyString) {
      splitResult = split(this, ((RubyString) r).toString());
    } else {
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "wrong argument type " + r.getRubyClass() + " (expected Regexp)");
    }

    RubyArray a = new RubyArray(splitResult.size());
    int i = 0;
    //        for (String str : splitResult) {
    for (Iterator iter = splitResult.iterator(); iter.hasNext(); ) {
      String str = (String) iter.next();
      if (!(bSkipFirstEmptyItem && 0 == i && (str == null || str.equals("")))) {
        // To conform ruby's behavior, discard the first empty element
        a.add(ObjectFactory.createString(str));
      }
      ++i;
    }
    return a;
  }
Exemplo n.º 2
0
 // @RubyLevelMethod(name="chomp!")
 public RubyValue chomp_danger(RubyArray args) {
   RubyValue separator = (null == args) ? GlobalVariables.get("$/") : args.get(0);
   if (chomp(((RubyString) separator).toString())) {
     return this;
   } else {
     return RubyConstant.QNIL;
   }
 }
Exemplo n.º 3
0
 // @RubyLevelMethod(name="chomp")
 public RubyValue chomp(RubyArray args) {
   RubyString string = ObjectFactory.createString(toString());
   RubyValue separator = (null != args) ? args.get(0) : GlobalVariables.get("$/");
   string.chomp(((RubyString) separator).toString());
   return string;
 }