/** num_coerce */ @JRubyMethod(name = "coerce") public IRubyObject coerce(IRubyObject other) { if (getMetaClass() == other.getMetaClass()) return getRuntime().newArray(other, this); IRubyObject cdr = RubyKernel.new_float(this, this); IRubyObject car = RubyKernel.new_float(this, other); return getRuntime().newArray(car, cdr); }
@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; ndigits = -ndigits; Ruby runtime = context.getRuntime(); if (ndigits < 0) throw runtime.newArgumentError("ndigits out of range"); 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 { IRubyObject h = f.callMethod(context, "/", RubyFixnum.two(runtime)); IRubyObject r = callMethod(context, "%", f); IRubyObject n = callMethod(context, "-", r); if (!r.callMethod(context, "<", h).isTrue()) n = n.callMethod(context, "+", f); return n; } }
@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; } }
/** rb_num2dbl and NUM2DBL */ public static double num2dbl(IRubyObject arg) { if (arg instanceof RubyFloat) { return ((RubyFloat) arg).getDoubleValue(); } else if (arg instanceof RubyString) { throw arg.getRuntime().newTypeError("no implicit conversion to float from string"); } else if (arg == arg.getRuntime().getNil()) { throw arg.getRuntime().newTypeError("no implicit conversion to float from nil"); } return RubyKernel.new_float(arg, arg).getDoubleValue(); }
@JRubyMethod(optional = 2, compat = CompatVersion.RUBY2_0) public IRubyObject backtrace_locations(ThreadContext context, IRubyObject[] args) { ThreadContext myContext = getContext(); if (myContext == null) return context.nil; Ruby runtime = context.runtime; Integer[] ll = RubyKernel.levelAndLengthFromArgs(runtime, args, 0); Integer level = ll[0], length = ll[1]; return myContext.createCallerLocations(level, length, getNativeThread().getStackTrace()); }
// c: rb_f_rand for 1.8 public static IRubyObject randCommon18( ThreadContext context, IRubyObject recv, IRubyObject[] args) { RandomType random = getDefaultRand(context); if (args.length == 0) { return randFloat(context, random); } IRubyObject arg = args[0]; if (arg.isNil()) { return randFloat(context, random); } // 1.8 calls rb_Integer() RubyInteger max = (RubyInteger) RubyKernel.new_integer(context, recv, arg); return randCommon(context, random, max); }
@JRubyMethod( name = {"call", "[]"}, rest = true) public IRubyObject call(ThreadContext context, IRubyObject[] args) { if (disabled) { RubyKernel.raise( context, context.runtime.getThreadError(), new IRubyObject[] { context.runtime.newString("continuations can not be called from outside their scope") }, Block.NULL_BLOCK); } continuation.args = args; throw continuation; }
@JRubyMethod(name = "backtrace", optional = 2, compat = CompatVersion.RUBY2_0) public IRubyObject backtrace20(ThreadContext context, IRubyObject[] args) { ThreadContext myContext = getContext(); // context can be nil if we have not started or GC has claimed our context if (myContext == null) return context.nil; Thread nativeThread = getNativeThread(); // nativeThread can be null if the thread has terminated and GC has claimed it if (nativeThread == null) return context.nil; Ruby runtime = context.runtime; Integer[] ll = RubyKernel.levelAndLengthFromArgs(runtime, args, 0); Integer level = ll[0], length = ll[1]; return myContext.createCallerBacktrace(level, length, getNativeThread().getStackTrace()); }
@JRubyMethod(optional = 3) public IRubyObject raise(IRubyObject[] args, Block block) { Ruby runtime = getRuntime(); ThreadContext context = runtime.getCurrentContext(); if (this == context.getThread()) { return RubyKernel.raise(context, runtime.getKernel(), args, block); } debug(this, "before raising"); RubyThread currentThread = getRuntime().getCurrentContext().getThread(); debug(this, "raising"); IRubyObject exception = prepareRaiseException(runtime, args, block); runtime .getThreadService() .deliverEvent( new ThreadService.Event( currentThread, this, ThreadService.Event.Type.RAISE, exception)); return this; }
@JRubyMethod(name = "printf", required = 1, rest = true) public IRubyObject printf(ThreadContext context, IRubyObject[] args) { append(context, RubyKernel.sprintf(context, this, args)); return getRuntime().getNil(); }
@JRubyMethod(name = "printf", required = 1, rest = true) public IRubyObject printf(ThreadContext context, IRubyObject[] args) throws IOException { write(RubyKernel.sprintf(context, this, args)); return context.getRuntime().getNil(); }
@JRubyMethod(name = "last", compat = CompatVersion.RUBY1_9) public IRubyObject last(ThreadContext context, IRubyObject arg) { return ((RubyArray) RubyKernel.new_array(context, this, this)).last(arg); }
/** flo_coerce */ @JRubyMethod(name = "coerce", required = 1) @Override public IRubyObject coerce(IRubyObject other) { return getRuntime().newArray(RubyKernel.new_float(this, other), this); }