Implementation of the Integer class.
| Method from org.jruby.RubyInteger Detail: |
public RubyString chr() {
if (getLongValue() < 0 || getLongValue() > 0xff) {
throw getRuntime().newRangeError(this.toString() + " out of char range");
}
return RubyString.newString(getRuntime(), new ByteList(new byte[]{(byte)getLongValue()}, false));
}
|
public RubyInteger convertToInteger() {
return this;
}
|
public static RubyClass createIntegerClass(Ruby runtime) {
RubyClass integer = runtime.defineClass("Integer", runtime.getNumeric(),
ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
runtime.setInteger(integer);
integer.kindOf = new RubyModule.KindOf() {
public boolean isKindOf(IRubyObject obj, RubyModule type) {
return obj instanceof RubyInteger;
}
};
integer.getSingletonClass().undefineMethod("new");
integer.includeModule(runtime.getPrecision());
integer.defineAnnotatedMethods(RubyInteger.class);
return integer;
}
|
public IRubyObject downto(ThreadContext context,
IRubyObject to,
Block block) {
final Ruby runtime = getRuntime();
if (this instanceof RubyFixnum && to instanceof RubyFixnum) {
RubyFixnum toFixnum = (RubyFixnum) to;
final long toValue = toFixnum.getLongValue();
if (block.getBody().getArgumentType() == BlockBody.ZERO_ARGS) {
final IRubyObject nil = runtime.getNil();
for (long i = getLongValue(); i >= toValue; i--) {
block.yield(context, nil);
}
} else {
for (long i = getLongValue(); i >= toValue; i--) {
block.yield(context, RubyFixnum.newFixnum(getRuntime(), i));
}
}
} else {
RubyNumeric i = this;
while (true) {
if (i.callMethod(context, MethodIndex.OP_LT, "< ", to).isTrue()) {
break;
}
block.yield(context, i);
i = (RubyNumeric) i.callMethod(context, MethodIndex.OP_MINUS, "-", RubyFixnum.one(getRuntime()));
}
}
return this;
}
|
public static RubyBoolean even_p(ThreadContext context,
IRubyObject recv) {
if(recv.callMethod(context, "%", recv.getRuntime().newFixnum(2)) == RubyFixnum.zero(recv.getRuntime())) {
return recv.getRuntime().getTrue();
}
return recv.getRuntime().getFalse();
}
|
public static IRubyObject induced_from(ThreadContext context,
IRubyObject recv,
IRubyObject other) {
if (other instanceof RubyFixnum || other instanceof RubyBignum) {
return other;
} else if (other instanceof RubyFloat) {
return other.callMethod(context, MethodIndex.TO_I, "to_i");
} else {
throw recv.getRuntime().newTypeError(
"failed to convert " + other.getMetaClass().getName() + " into Integer");
}
}
|
public IRubyObject integer_p() {
return getRuntime().getTrue();
}
|
public static RubyBoolean odd_p(ThreadContext context,
IRubyObject recv) {
if(recv.callMethod(context, "%", recv.getRuntime().newFixnum(2)) != RubyFixnum.zero(recv.getRuntime())) {
return recv.getRuntime().getTrue();
}
return recv.getRuntime().getFalse();
}
|
public static IRubyObject pred(ThreadContext context,
IRubyObject recv) {
return recv.callMethod(context, "-", recv.getRuntime().newFixnum(1));
}
|
public IRubyObject succ(ThreadContext context) {
if (this instanceof RubyFixnum) {
return RubyFixnum.newFixnum(getRuntime(), getLongValue() + 1L);
} else {
return callMethod(context, MethodIndex.OP_PLUS, "+", RubyFixnum.one(getRuntime()));
}
}
|
public IRubyObject times(ThreadContext context,
Block block) {
final Ruby runtime = context.getRuntime();
if (this instanceof RubyFixnum) {
final long value = getLongValue();
if (block.getBody().getArgumentType() == BlockBody.ZERO_ARGS) {
final IRubyObject nil = runtime.getNil();
for (long i = 0; i < value; i++) {
block.yield(context, nil);
}
} else {
for (long i = 0; i < value; i++) {
block.yield(context, RubyFixnum.newFixnum(runtime, i));
}
}
} else {
RubyNumeric i = RubyFixnum.zero(runtime);
while (true) {
if (!i.callMethod(context, MethodIndex.OP_LT, "< ", this).isTrue()) {
break;
}
block.yield(context, i);
i = (RubyNumeric) i.callMethod(context, MethodIndex.OP_PLUS, "+", RubyFixnum.one(runtime));
}
}
return this;
}
|
protected RubyFloat toFloat() {
return RubyFloat.newFloat(getRuntime(), getDoubleValue());
}
|
public RubyInteger to_i() {
return this;
}
|
public IRubyObject upto(ThreadContext context,
IRubyObject to,
Block block) {
final Ruby runtime = getRuntime();
if (this instanceof RubyFixnum && to instanceof RubyFixnum) {
RubyFixnum toFixnum = (RubyFixnum) to;
final long toValue = toFixnum.getLongValue();
final long fromValue = getLongValue();
if (block.getBody().getArgumentType() == BlockBody.ZERO_ARGS) {
final IRubyObject nil = runtime.getNil();
for (long i = fromValue; i < = toValue; i++) {
block.yield(context, nil);
}
} else {
for (long i = fromValue; i < = toValue; i++) {
block.yield(context, RubyFixnum.newFixnum(runtime, i));
}
}
} else {
RubyNumeric i = this;
while (true) {
if (i.callMethod(context, MethodIndex.OP_GT, " >", to).isTrue()) {
break;
}
block.yield(context, i);
i = (RubyNumeric) i.callMethod(context, MethodIndex.OP_PLUS, "+", RubyFixnum.one(runtime));
}
}
return this;
}
|