private void aggregateDimensions(IRubyObject[] sizes) { int[] newDimensions = new int[dimensions.length + sizes.length]; System.arraycopy(dimensions, 0, newDimensions, 0, dimensions.length); for (int i = 0; i < sizes.length; i++) { IRubyObject size = sizes[i]; int intSize = (int) size.convertToInteger().getLongValue(); newDimensions[i + dimensions.length] = intSize; } dimensions = newDimensions; } }
/** int_downto * */ // TODO: Make callCoerced work in block context...then fix downto, step, and upto. @JRubyMethod public IRubyObject downto(ThreadContext context, IRubyObject to, Block block) { if (block.isGiven()) { if (this instanceof RubyFixnum && to instanceof RubyFixnum) { fixnumDownto(context, ((RubyFixnum)this).getLongValue(), ((RubyFixnum)to).getLongValue(), block); } else { duckDownto(context, this, to, block); } return this; } else { return enumeratorizeWithSize(context, this, "downto", new IRubyObject[] { to }, downToSize(context, this, to)); } }
/** int_upto * */ @JRubyMethod public IRubyObject upto(ThreadContext context, IRubyObject to, Block block) { if (block.isGiven()) { if (this instanceof RubyFixnum && to instanceof RubyFixnum) { fixnumUpto(context, ((RubyFixnum)this).getLongValue(), ((RubyFixnum)to).getLongValue(), block); } else { duckUpto(context, this, to, block); } return this; } else { return enumeratorizeWithSize(context, this, "upto", new IRubyObject[] { to }, uptoSize(context, this, to)); } }
protected static int nonFixnumHashCode(IRubyObject hashValue) { RubyInteger integer = hashValue.convertToInteger(); if (integer instanceof RubyBignum) { return integer.getBigIntegerValue().intValue(); } return (int) integer.getLongValue(); }
protected RubyFloat toFloat() { return RubyFloat.newFloat(getRuntime(), getDoubleValue()); }
private static int simpleIntDiff(IRubyObject n) { final int days = n.convertToInteger().getIntValue(); if (n instanceof RubyRational) { if (((RubyRational) n).getDenominator().getLongValue() != 1) { return days + 1; // MRI rulez: 1/2 -> 1 (but 0.5 -> 0) } } return days; }
public RubyNumeric roundShared(ThreadContext context, int ndigits, RoundingMode roundingMode) { Ruby runtime = context.runtime; RubyNumeric f, h, n, r; if (int_round_zero_p(context, ndigits)) { return RubyFixnum.zero(runtime); } f = (RubyNumeric) int_pow(context, 10, -ndigits); if (this instanceof RubyFixnum && f instanceof RubyFixnum) { long x = fix2long(this), y = fix2long(f); boolean neg = x < 0; if (neg) x = -x; x = doRound(context, roundingMode, x, y); if (neg) x = -x; return RubyFixnum.newFixnum(runtime, x); } if (f instanceof RubyFloat) { /* then int_pow overflow */ return RubyFixnum.zero(runtime); } h = (RubyNumeric) f.idiv(context, 2); r = (RubyNumeric) this.op_mod(context, f); n = (RubyNumeric) this.op_minus(context, r); r = (RubyNumeric) r.op_cmp(context, h); if (r.isPositive(context).isTrue() || (r.isZero() && doRoundCheck(context, roundingMode, this, n, f))) { n = (RubyNumeric) n.op_plus(context, f); } return n; }
boolean isOne() { return getBigIntegerValue().equals(BigInteger.ONE); }
@JRubyMethod(name = "alloc", meta = true) public static RubyString alloc(ThreadContext context, IRubyObject recv, IRubyObject size) { return RubyString.newStringLight(context.runtime, RubyInteger.fix2int(size)); }
/** int_downto * */ // TODO: Make callCoerced work in block context...then fix downto, step, and upto. @JRubyMethod public IRubyObject downto(ThreadContext context, IRubyObject to, Block block) { if (block.isGiven()) { if (this instanceof RubyFixnum && to instanceof RubyFixnum) { fixnumDownto(context, ((RubyFixnum)this).getLongValue(), ((RubyFixnum)to).getLongValue(), block); } else { duckDownto(context, this, to, block); } return this; } else { return enumeratorize(context.runtime, this, "downto", to); } }
/** int_upto * */ @JRubyMethod public IRubyObject upto(ThreadContext context, IRubyObject to, Block block) { if (block.isGiven()) { if (this instanceof RubyFixnum && to instanceof RubyFixnum) { fixnumUpto(context, ((RubyFixnum)this).getLongValue(), ((RubyFixnum)to).getLongValue(), block); } else { duckUpto(context, this, to, block); } return this; } else { return enumeratorize(context.runtime, this, "upto", to); } }
@JRubyMethod(name = "quo", required = 1, compat = RUBY1_9) public IRubyObject quo19(ThreadContext context, IRubyObject other) { if (other instanceof RubyInteger && ((RubyInteger) other).getDoubleValue() == 0) { throw context.runtime.newZeroDivisionError(); } return quo(context, other); }
RubyInteger.createIntegerClass(this);
private void fixnumEach(ThreadContext context, Block block) { // We must avoid integer overflows. long to = ((RubyFixnum) end).getLongValue(); if (isExclusive) { if (to == Long.MIN_VALUE) { return; } to--; } RubyInteger.fixnumUpto(context, ((RubyFixnum) begin).getLongValue(), to, block); }
@JRubyMethod(name = "gets", writes = FrameField.LASTLINE, compat = RUBY1_9) public IRubyObject gets19(ThreadContext context, IRubyObject arg) { Ruby runtime = context.runtime; ByteList separator; long limit = -1; IRubyObject test = TypeConverter.checkIntegerType(runtime, arg, "to_int"); if (test instanceof RubyInteger) { limit = RubyInteger.fix2long(test); separator = separator(runtime); } else { separator = separator(runtime, arg); } IRubyObject result = getline(context, separator, limit); if (!result.isNil()) context.setLastLine(result); return result; }
protected static int nonFixnumHashCode(IRubyObject hashValue) { RubyInteger integer = hashValue.convertToInteger(); if (integer instanceof RubyBignum) { return integer.getBigIntegerValue().intValue(); } return (int) integer.getLongValue(); }
public RubyNumeric roundShared(ThreadContext context, int ndigits, RoundingMode roundingMode) { Ruby runtime = context.runtime; RubyNumeric f, h, n, r; if (int_round_zero_p(context, ndigits)) { return RubyFixnum.zero(runtime); } f = (RubyNumeric) int_pow(context, 10, -ndigits); if (this instanceof RubyFixnum && f instanceof RubyFixnum) { long x = fix2long(this), y = fix2long(f); boolean neg = x < 0; if (neg) x = -x; x = doRound(context, roundingMode, x, y); if (neg) x = -x; return RubyFixnum.newFixnum(runtime, x); } if (f instanceof RubyFloat) { /* then int_pow overflow */ return RubyFixnum.zero(runtime); } h = (RubyNumeric) f.idiv(context, 2); r = (RubyNumeric) this.op_mod(context, f); n = (RubyNumeric) this.op_minus(context, r); r = (RubyNumeric) r.op_cmp(context, h); if (r.isPositive(context).isTrue() || (r.isZero() && doRoundCheck(context, roundingMode, this, n, f))) { n = (RubyNumeric) n.op_plus(context, f); } return n; }
boolean isOne() { return getBigIntegerValue().equals(BigInteger.ONE); }
protected RubyFloat toFloat() { return RubyFloat.newFloat(getRuntime(), getDoubleValue()); }
@JRubyMethod(name = "alloc", meta = true) public static RubyString alloc(ThreadContext context, IRubyObject recv, IRubyObject size) { return RubyString.newStringLight(context.runtime, RubyInteger.fix2int(size)); }