Example #1
0
  @JRubyMethod(name = "truncate", required = 1)
  public IRubyObject truncate(IRubyObject arg) {
    checkWritable();

    int len = RubyFixnum.fix2int(arg);
    if (len < 0) {
      throw getRuntime().newErrnoEINVALError("negative legnth");
    }

    internal.modify();
    internal.getByteList().length(len);
    return arg;
  }
Example #2
0
  /** flo_cmp */
  @JRubyMethod(name = "<=>", required = 1)
  public IRubyObject op_cmp(ThreadContext context, IRubyObject other) {
    switch (other.getMetaClass().getClassIndex()) {
      case FIXNUM:
      case BIGNUM:
        if (Double.isInfinite(value)) {
          return value > 0.0 ? RubyFixnum.one(getRuntime()) : RubyFixnum.minus_one(getRuntime());
        }
      case FLOAT:
        double b = ((RubyNumeric) other).getDoubleValue();
        return dbl_cmp(getRuntime(), value, b);
      default:
        if (Double.isInfinite(value) && other.respondsTo("infinite?")) {
          IRubyObject infinite = other.callMethod(context, "infinite?");
          if (infinite.isNil()) {
            return value > 0.0 ? RubyFixnum.one(getRuntime()) : RubyFixnum.minus_one(getRuntime());
          } else {
            int sign = RubyFixnum.fix2int(infinite);

            if (sign > 0) {
              if (value > 0.0) {
                return RubyFixnum.zero(getRuntime());
              } else {
                return RubyFixnum.minus_one(getRuntime());
              }
            } else {
              if (value < 0.0) {
                return RubyFixnum.zero(getRuntime());
              } else {
                return RubyFixnum.one(getRuntime());
              }
            }
          }
        }
        return coerceCmp(context, "<=>", other);
    }
  }