예제 #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);
  }
예제 #2
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);
 }
 @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());
 }
  @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));
  }
예제 #5
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;
  }
예제 #6
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;
    }
  }
예제 #7
0
 // @RubyLevelMethod(name="=~")
 public RubyValue operator_match(RubyValue arg) {
   if (arg instanceof RubyRegexp) {
     RubyRegexp reg = (RubyRegexp) arg;
     int p = reg.matchPosition(toString());
     if (p >= 0) {
       return ObjectFactory.createFixnum(p);
     } else {
       return RubyConstant.QNIL;
     }
   } else {
     return RubyAPI.callPublicOneArgMethod(arg, this, null, RubyID.matchID);
   }
 }
예제 #8
0
  // @RubyLevelMethod(name="casecmp")
  RubyValue run(RubyValue arg) {
    if (!(arg instanceof RubyString)) {
      return RubyConstant.QNIL;
    }

    RubyString value2 = (RubyString) arg;
    int compare = toString().toUpperCase().compareTo(value2.toString().toUpperCase());
    if (compare > 0) {
      compare = 1;
    } else if (compare < 0) {
      compare = -1;
    }
    return ObjectFactory.createFixnum(compare);
  }
예제 #9
0
  // @RubyLevelMethod(name="count")
  public RubyValue count(RubyArray args) {
    if (null == args) {
      throw new RubyException(RubyRuntime.ArgumentErrorClass, "wrong number of arguments");
    }

    // TODO incomplete
    int n = 0;
    //        for (RubyValue v : args) {
    for (Iterator iter = args.iterator(); iter.hasNext(); ) {
      RubyValue v = (RubyValue) iter.next();
      RubyString other_str = (RubyString) v;
      n += count(other_str.toString());
    }
    return ObjectFactory.createFixnum(n);
  }
예제 #10
0
파일: RubyFixnum.java 프로젝트: rzel/xruby
  private RubyValue rshift(int i) {
    if (i == 0) {
      return this;
    } else if (i < 0) {
      return lshift(-i);
    }

    if (i >= BIT_SIZE - 1) {
      if (this.value_ < 0) {
        return ObjectFactory.FIXNUM_NEGATIVE_ONE;
      }

      return ObjectFactory.FIXNUM0;
    }

    return ObjectFactory.createFixnum(this.value_ >> i);
  }
예제 #11
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);
    }
  }
예제 #12
0
  // @RubyLevelMethod(name="each_byte")
  public RubyValue each_byte(RubyBlock block) {
    if (block == null) return this;

    String string = toString();
    byte bytes[] = null;
    try {
      bytes = string.getBytes("UTF-8");
    } catch (java.io.UnsupportedEncodingException exc) {
    }

    for (int i = 0; bytes != null && i < bytes.length; ++i) {
      int nByte = bytes[i];
      if (nByte < 0) nByte = 256 + nByte;

      block.invoke(this, ObjectFactory.createFixnum(nByte));
    }

    return this;
  }
예제 #13
0
 // @RubyLevelMethod(name="new", singleton=true)
 public static RubyValue newArray(RubyValue receiver, RubyArray args, RubyBlock block) {
   RubyArray a;
   if (null == args) {
     a = new RubyArray();
   } else if (null == block) {
     if (args.get(0) instanceof RubyArray) {
       a = (RubyArray) args.get(0).clone();
     } else {
       RubyFixnum size = (RubyFixnum) args.get(0);
       RubyValue default_value = args.get(1);
       a = ObjectFactory.createArray(size.toInt(), default_value);
     }
   } else {
     RubyFixnum size = (RubyFixnum) args.get(0);
     a = new RubyArray();
     for (int i = 0; i < size.toFloat(); i++) {
       RubyValue return_value = block.invoke(receiver, ObjectFactory.createFixnum(i));
       a.add(return_value);
     }
   }
   a.setRubyClass((RubyClass) receiver);
   return a;
 }
예제 #14
0
 // @RubyLevelMethod(name="rindex")
 public RubyValue rindex(RubyValue arg) {
   for (int i = size() - 1; i >= 0; i--) {
     if (get(i).equals(arg)) return ObjectFactory.createFixnum(i);
   }
   return RubyConstant.QNIL;
 }
예제 #15
0
 // @RubyLevelMethod(name="hash")
 public RubyFixnum hash() {
   return ObjectFactory.createFixnum(hashCode());
 }
예제 #16
0
 // @RubyLevelMethod(name="length")
 public RubyFixnum length() {
   return ObjectFactory.createFixnum(this.array_.size());
 }
예제 #17
0
 // @RubyLevelMethod(name="bytesize")
 public RubyFixnum rubyBytesize() {
   return ObjectFactory.createFixnum(sb_.length());
 }
예제 #18
0
파일: RubyFixnum.java 프로젝트: rzel/xruby
 @RubyLevelMethod(name = "-@")
 public RubyValue uminus() {
   return ObjectFactory.createFixnum(-this.value_);
 }
예제 #19
0
  // @RubyLevelMethod(name="ord")
  public RubyValue ord() {
    if (sb_ == null || sb_.length() == 0) return ObjectFactory.createFixnum(0);

    return ObjectFactory.createFixnum(sb_.charAt(0));
  }