예제 #1
0
파일: RubyFixnum.java 프로젝트: rzel/xruby
  protected RubyValue doStep(RubyValue toArg, RubyValue stepArg, RubyBlock block) {
    if ((toArg instanceof RubyFixnum) && (stepArg instanceof RubyFixnum)) {
      int i = this.value_;
      int end = ((RubyFixnum) toArg).value_;
      int diff = ((RubyFixnum) stepArg).value_;
      if (diff > 0) {
        while (i <= end) {
          RubyValue v = block.invoke(this, ObjectFactory.createFixnum(i));
          if (block.breakedOrReturned()) {
            return v;
          }
          i += diff;
        }
      } else {
        while (i >= end) {
          RubyValue v = block.invoke(this, ObjectFactory.createFixnum(i));
          if (block.breakedOrReturned()) {
            return v;
          }
          i += diff;
        }
      }

      return this;
    }

    return super.doStep(toArg, stepArg, block);
  }
  @RubyLevelMethod(name = "open")
  public static RubyValue open(RubyValue receiver, RubyArray args, RubyBlock block) {
    String filename = args.get(0).toStr();
    RubyIO io;
    if (args.size() <= 1) {
      io = ObjectFactory.createFile(filename, "r");
    } else if (args.get(1) instanceof RubyFixnum) {
      String mode = "r";
      int i = args.get(1).toInt();
      if ((i & RDWR) != 0) {
        mode = mode + "w";
      }
      io = ObjectFactory.createFile(filename, mode);
    } else {
      RubyString mode = (RubyString) args.get(1);
      io = ObjectFactory.createFile(filename, mode.toString());
    }

    if (null == block) {
      return io;
    } else {
      RubyValue v = block.invoke(receiver, io);
      io.close();
      return v;
    }
  }
예제 #3
0
파일: RubyFixnum.java 프로젝트: rzel/xruby
  @RubyLevelMethod(name = ">")
  public RubyValue opGt(RubyValue v) {
    if (v instanceof RubyFixnum) {
      return ObjectFactory.createBoolean(this.value_ > ((RubyFixnum) v).value_);
    } else if (v instanceof RubyFloat) {
      return ObjectFactory.createBoolean(this.value_ > v.toFloat());
    }

    return coerceRelop(RubyID.gtID, v);
  }
예제 #4
0
  // @RubyLevelMethod(name="downcase")
  public RubyString downcase() {
    int length = this.sb_.length();
    if (length == 0) {
      return ObjectFactory.createString();
    }

    char[] ca = new char[length];
    this.sb_.getChars(0, length, ca, 0);

    for (int i = 0; i < ca.length; i++) {
      ca[i] = Character.toLowerCase(ca[i]);
    }

    return ObjectFactory.createString(new StringBuffer().append(ca));
  }
예제 #5
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)");
    }
  }
예제 #6
0
 // @RubyLevelMethod(name="tr_s")
 public RubyValue tr_s(RubyValue arg1, RubyValue arg2) {
   RubyString string = ObjectFactory.createString(toString());
   RubyString from = (RubyString) arg1;
   RubyString to = (RubyString) arg2;
   string.tr_s(from.toString(), to.toString());
   return string;
 }
예제 #7
0
 // @RubyLevelMethod(name="nitems")
 public RubyValue nitems() {
   int n = 0;
   for (int i = 0; i < size(); i++) {
     if (get(i) != RubyConstant.QNIL) n++;
   }
   return ObjectFactory.createFixnum(n);
 }
예제 #8
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;
  }
  @RubyLevelMethod(name = "trace_var", module = true)
  public static RubyValue trace_var(RubyValue receiver, RubyArray args, RubyBlock block) {
    if (null == args || args.size() < 1) {
      int actual_argc = (null == args) ? 0 : args.size();
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "in `trace_var': wrong number of arguments (" + actual_argc + " for 1)");
    }

    if (!(args.get(0) instanceof RubySymbol)) {
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass, args.get(0).toString() + " is not a symbol");
    }

    String name = ((RubySymbol) args.get(0)).toString();

    RubyValue v = args.get(1);
    if (v instanceof RubyProc) {
      GlobalVariables.addTraceProc(name, (RubyProc) v);
    } else if (null != block) {
      GlobalVariables.addTraceProc(name, ObjectFactory.createProc(block));
    } else {
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass, "tried to create Proc object without a block");
    }

    return RubyConstant.QNIL;
  }
예제 #10
0
 // @RubyLevelMethod(name="to_f")
 public RubyFloat toRubyFloat() {
   double d;
   try {
     d = this.toFloat();
   } catch (NumberFormatException e) {
     throw new RubyException(RubyRuntime.ArgumentErrorClass, e.toString());
   }
   return ObjectFactory.createFloat(d);
 }
예제 #11
0
파일: RubyFixnum.java 프로젝트: rzel/xruby
  @RubyLevelMethod(name = "to_s")
  public RubyString to_s(RubyValue v) {
    int radix = v.toInt();
    if (radix < 2 || radix > 36) {
      throw new RubyException(RubyRuntime.ArgumentErrorClass, "illegal radix " + radix);
    }

    return ObjectFactory.createString(this.toString(radix));
  }
 @RubyLevelMethod(
     name = "object_id",
     alias = {"__id__", "hash"})
 public static RubyValue objectId(RubyValue receiver) {
   // Object.hashCode() javadoc:
   // As much as is reasonably practical, the hashCode method defined
   // by class Object does return distinct integers for distinct objects.
   return ObjectFactory.createFixnum(receiver.hashCode());
 }
예제 #13
0
  public RubyValue pre_match() {
    String res = "";
    int nMatchEnd = result_.beginOffset(0);
    if (nMatchEnd >= 0 && nMatchEnd < str_.length()) res = str_.substring(0, nMatchEnd);

    if (res == null) return RubyConstant.QNIL;

    return ObjectFactory.createString(res);
  }
예제 #14
0
  RubyValue getRetValue() {
    RubyValue ret = RubyConstant.QNIL;
    if (isNoThreadedMode()) {
      ret = ObjectFactory.createString(getSyncEngine().getNotify().getNotifyBody());
      getSyncEngine().getNotify().cleanNotifyBody();
    }

    return ret;
  }
 @RubyLevelMethod(name = "===")
 public static RubyValue objEqual(RubyValue receiver, RubyValue arg) {
   if (receiver == arg) {
     return RubyConstant.QTRUE;
   } else {
     boolean result = RubyAPI.callPublicOneArgMethod(receiver, arg, null, RubyID.equalID).isTrue();
     return ObjectFactory.createBoolean(result);
   }
 }
예제 #16
0
  public RubyValue post_match() {
    String res = "";
    int nMatchEnd = result_.endOffset(result_.groups() - 1);
    if (nMatchEnd >= 0 && nMatchEnd + 1 < str_.length()) res = str_.substring(nMatchEnd);

    if (res == null) return RubyConstant.QNIL;

    return ObjectFactory.createString(res);
  }
  @RubyLevelMethod(name = "at_exit", module = true)
  public static RubyValue atExit(RubyValue receiver, RubyBlock block) {
    if (null == block) {
      throw new RubyException(RubyRuntime.ArgumentErrorClass, "called without a block");
    }

    AtExitBlocks.registerBlock(block);
    return ObjectFactory.createProc(block);
  }
예제 #18
0
  public RubyValue captures() {
    RubyArray ar = new RubyArray();
    for (int i = 1; i < result_.groups(); i++) {
      String res = result_.group(i);
      if (res == null) ar.add(RubyConstant.QNIL);
      else ar.add(ObjectFactory.createString(res));
    }

    return ar;
  }
 @RubyLevelMethod(name = "gets", module = true)
 public static RubyValue gets(RubyValue receiver) {
   String s = null;
   try {
     s = in_.readLine();
   } catch (IOException e) {
   }
   GlobalVariables.set((null == s ? RubyConstant.QNIL : ObjectFactory.createString(s)), "$_");
   return GlobalVariables.get("$_");
 }
 @RubyLevelMethod(name = "sprintf", module = true)
 public static RubyValue sprintf(RubyValue receiver, RubyArray args) {
   String fmt = args.get(0).toStr();
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   PrintStream ps = new PrintStream(baos);
   ps.printf(fmt, RubyKernelModule.buildFormatArg(args, 1));
   ps.flush();
   ps.close();
   return ObjectFactory.createString(baos.toString());
 }
예제 #21
0
  public RubyString appendString(RubyValue v) {
    if (v instanceof RubyString) {
      return appendString((RubyString) v);
    } else {
      RubyValue r = RubyAPI.callPublicNoArgMethod(v, null, RubyID.toSID);
      if (r instanceof RubyString) return appendString((RubyString) r);

      return ObjectFactory.createString(r.toString());
    }
  }
  @RubyLevelMethod(name = "sleep", module = true)
  public static RubyValue sleep(RubyValue receiver, RubyValue arg) {
    long milliseconds = RubyTypesUtil.convertToJavaLong(arg) * 1000;
    long startTime = System.currentTimeMillis();

    RubyThread.sleep(milliseconds);

    long endTime = System.currentTimeMillis();
    return ObjectFactory.createFixnum((int) Math.round((endTime - startTime) / 1000.0));
  }
예제 #23
0
  public RubyValue to_a() {
    RubyArray ar = new RubyArray();
    for (int i = 0; i < result_.groups(); i++) {
      String str = result_.group(i);
      if (str != null) ar.add(ObjectFactory.createString(str));
      else ar.add(RubyConstant.QNIL);
    }

    return ar;
  }
예제 #24
0
  // @RubyLevelMethod(name="[]")
  public RubyValue array_access(RubyArray args) {
    String string = toString();
    if (args.size() == 1) {
      RubyValue arg = args.get(0);
      if (arg instanceof RubyString) {
        String str = ((RubyString) arg).toString();
        if (string.indexOf(str) >= 0) {
          return ObjectFactory.createString(str);
        } else {
          return RubyConstant.QNIL;
        }
      } else if (arg instanceof RubyRange) {
        RubyRange range = (RubyRange) arg;
        int start = range.getLeft().toInt();
        int end = range.getRight().toInt();
        return substring(string, start, end, range.isExcludeEnd());
      } else if (arg instanceof RubyRegexp) {
        RubyRegexp regexp = (RubyRegexp) arg;
        RubyMatchData match = regexp.match(string);
        if (match != null) {
          return ObjectFactory.createString(match.toString());
        } else {
          return RubyConstant.QNIL;
        }
      } else {
        int index = arg.toInt();
        if (index < 0) {
          index = string.length() + index;
        }

        if (index < 0 || index >= string.length()) {
          return RubyConstant.QNIL;
        } else {
          return ObjectFactory.createFixnum(string.charAt(index));
        }
      }
    } else {
      int start = args.get(0).toInt();
      int length = args.get(1).toInt() - 1;

      return substring(string, start, start + length, false);
    }
  }
예제 #25
0
  // @RubyLevelMethod(name="delete")
  public RubyValue delete(RubyArray args) {
    if (null == args) {
      throw new RubyException(RubyRuntime.ArgumentErrorClass, "wrong number of arguments");
    }

    RubyString string = ObjectFactory.createString(toString());
    String arg = ((RubyString) args.get(0)).toString();
    string.delete(arg);
    return string;
  }
예제 #26
0
파일: RubyFixnum.java 프로젝트: rzel/xruby
  @RubyLevelMethod(name = "quo")
  public RubyFloat quo(RubyValue v) {
    if (v instanceof RubyFixnum) {
      return ObjectFactory.createFloat(this.value_ / ((RubyFixnum) v).value_);
    }

    // FIXME: should be coerced.
    throw new RubyException(
        RubyRuntime.TypeErrorClass, v.getRubyClass().getName() + " can't be coersed into Fixnum");
  }
예제 #27
0
파일: RubyFixnum.java 프로젝트: rzel/xruby
  public RubyValue times(RubyBlock block) {
    int value = this.value_;
    for (int i = 0; i < value; ++i) {
      RubyValue v = block.invoke(this, ObjectFactory.createFixnum(i));
      if (block.breakedOrReturned()) {
        return v;
      }
    }

    return this;
  }
예제 #28
0
  // @RubyLevelMethod(name="swapcase")
  public RubyString swapcase() {
    int length = this.sb_.length();
    if (length == 0) {
      return ObjectFactory.createString();
    }

    char[] ca = new char[length];
    this.sb_.getChars(0, length, ca, 0);

    for (int i = 0; i < length; i++) {
      char c = ca[i];
      if (Character.isUpperCase(c)) {
        ca[i] = Character.toLowerCase(c);
      } else if (Character.isLowerCase(c)) {
        ca[i] = Character.toUpperCase(c);
      }
    }

    return ObjectFactory.createString(new StringBuffer().append(ca));
  }
예제 #29
0
파일: RubyFixnum.java 프로젝트: rzel/xruby
  @RubyLevelMethod(name = "==")
  public RubyValue opEqual(RubyValue arg) {
    if (arg == this) {
      return RubyConstant.QTRUE;
    }

    if (arg instanceof RubyFixnum) {
      return ObjectFactory.createBoolean(this.value_ == ((RubyFixnum) arg).value_);
    }

    return RubyAPI.callOneArgMethod(arg, this, null, RubyID.equalID);
  }
예제 #30
0
  // @RubyLevelMethod(name="hex")
  public RubyValue hex() {
    String s = toString();
    if (s.startsWith("0x")) {
      s = s.substring("0x".length());
    }

    try {
      return ObjectFactory.createFixnum(Long.parseLong(s, 16));
    } catch (NumberFormatException e) {
      return ObjectFactory.FIXNUM0;
    }
  }