Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2245c4f

Browse files
committed
invent a signum() helper (for now only on Integer) and start using it
1 parent 3dfeb12 commit 2245c4f

File tree

5 files changed

+12
-5
lines changed

5 files changed

+12
-5
lines changed

core/src/main/java/org/jruby/RubyBignum.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ public BigInteger getValue() {
138138
return value;
139139
}
140140

141+
@Override
142+
public int signum() { return value.signum(); }
143+
141144
/* ================
142145
* Utility Methods
143146
* ================
@@ -255,8 +258,7 @@ public IRubyObject to_s(IRubyObject[] args) {
255258
@JRubyMethod(name = "to_s", alias = "inspect")
256259
@Override
257260
public IRubyObject to_s() {
258-
int base = 10;
259-
return RubyString.newUSASCIIString(getRuntime(), getValue().toString(base));
261+
return RubyString.newUSASCIIString(getRuntime(), getValue().toString(10));
260262
}
261263

262264
@JRubyMethod(name = "to_s")

core/src/main/java/org/jruby/RubyFixnum.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ public BigInteger getBigIntegerValue() {
199199
return BigInteger.valueOf(value);
200200
}
201201

202+
@Override
203+
public int signum() { return Long.signum(value); }
204+
202205
public static RubyFixnum newFixnum(Ruby runtime, long value) {
203206
if (USE_CACHE && isInCacheRange(value)) {
204207
return cachedFixnum(runtime, value);

core/src/main/java/org/jruby/RubyInteger.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ protected RubyFloat toFloat() {
110110
return RubyFloat.newFloat(getRuntime(), getDoubleValue());
111111
}
112112

113+
public int signum() { return getBigIntegerValue().signum(); }
114+
113115
/* ================
114116
* Instance Methods
115117
* ================

core/src/main/java/org/jruby/RubyRational.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ public IRubyObject op_expt(ThreadContext context, IRubyObject other) {
632632

633633
// General case
634634
if (other instanceof RubyFixnum || other instanceof RubyBignum) {
635-
return fix_expt(context, (RubyInteger) other, ((RubyInteger) other).getBigIntegerValue().signum());
635+
return fix_expt(context, (RubyInteger) other, ((RubyInteger) other).signum());
636636
}
637637
if (other instanceof RubyFloat || other instanceof RubyRational) {
638638
return f_expt(context, f_to_f(context, this), other);

core/src/main/java/org/jruby/util/Numeric.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,15 +334,15 @@ public static IRubyObject f_lshift(ThreadContext context, IRubyObject x, IRubyOb
334334
*
335335
*/
336336
public static boolean f_negative_p(ThreadContext context, IRubyObject x) {
337-
if (x instanceof RubyFixnum) return ((RubyFixnum)x).getLongValue() < 0;
337+
if (x instanceof RubyInteger) return ((RubyInteger) x).signum() == -1;
338338
return sites(context).op_lt.call(context, x, x, RubyFixnum.zero(context.runtime)).isTrue();
339339
}
340340

341341
/** f_zero_p
342342
*
343343
*/
344344
public static boolean f_zero_p(ThreadContext context, IRubyObject x) {
345-
if (x instanceof RubyFixnum) return ((RubyFixnum)x).getLongValue() == 0;
345+
if (x instanceof RubyInteger) return ((RubyInteger) x).signum() == 0;
346346
return sites(context).op_equals.call(context, x, x, RubyFixnum.zero(context.runtime)).isTrue();
347347
}
348348

0 commit comments

Comments
 (0)