Example #1
0
  @JRubyMethod(name = "initialize", optional = 2, frame = true, visibility = Visibility.PRIVATE)
  public IRubyObject initialize(IRubyObject[] args, Block unusedBlock) {
    Object modeArgument = null;
    switch (args.length) {
      case 0:
        internal = RubyString.newEmptyString(getRuntime());
        modeArgument = "r+";
        break;
      case 1:
        internal = args[0].convertToString();
        modeArgument = internal.isFrozen() ? "r" : "r+";
        break;
      case 2:
        internal = args[0].convertToString();
        if (args[1] instanceof RubyFixnum) {
          modeArgument = RubyFixnum.fix2long(args[1]);
        } else {
          modeArgument = args[1].convertToString().toString();
        }
        break;
    }

    initializeModes(modeArgument);

    if (modes.isWritable() && internal.isFrozen()) {
      throw getRuntime().newErrnoEACCESError("Permission denied");
    }

    if (modes.isTruncate()) {
      internal.modifyCheck();
      internal.empty();
    }

    return this;
  }
Example #2
0
  @JRubyMethod(name = "round", compat = CompatVersion.RUBY1_9)
  public IRubyObject round19(ThreadContext context, IRubyObject arg) {
    int ndigits = RubyNumeric.num2int(arg);
    if (ndigits > 0) return RubyKernel.new_float(this, this);
    if (ndigits == 0) return this;
    Ruby runtime = context.runtime;

    long bytes = (this instanceof RubyFixnum) ? 8 : RubyFixnum.fix2long(callMethod("size"));
    /* If 10**N/2 > this, return 0 */
    /* We have log_256(10) > 0.415241 and log_256(1/2)=-0.125 */
    if (-0.415241 * ndigits - 0.125 > bytes) {
      return RubyFixnum.zero(runtime);
    }

    IRubyObject f = Numeric.int_pow(context, 10, -ndigits);

    if (this instanceof RubyFixnum && f instanceof RubyFixnum) {
      long x = ((RubyFixnum) this).getLongValue();
      long y = ((RubyFixnum) f).getLongValue();
      boolean neg = x < 0;
      if (neg) x = -x;
      x = (x + y / 2) / y * y;
      if (neg) x = -x;
      return RubyFixnum.newFixnum(runtime, x);
    } else if (f instanceof RubyFloat) {
      return RubyFixnum.zero(runtime);
    } else {
      IRubyObject h = f.callMethod(context, "/", RubyFixnum.two(runtime));
      IRubyObject r = callMethod(context, "%", f);
      IRubyObject n = callMethod(context, "-", r);
      String op = callMethod(context, "<", RubyFixnum.zero(runtime)).isTrue() ? "<=" : "<";
      if (!r.callMethod(context, op, h).isTrue()) n = n.callMethod(context, "+", f);
      return n;
    }
  }