summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--numeric.c29
2 files changed, 25 insertions, 9 deletions
diff --git a/ChangeLog b/ChangeLog
index eacc6dc715..c3516959ee 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Tue Mar 11 19:48:09 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * numeric.c (fix_coerce): try conversion before type check.
+ [ruby-core:15838]
+
Tue Mar 11 17:03:23 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
* lib/delegate.rb (Delegator#initialize, DelegateClass): skip correct
diff --git a/numeric.c b/numeric.c
index b588081f23..7abc605088 100644
--- a/numeric.c
+++ b/numeric.c
@@ -1538,6 +1538,7 @@ long
rb_num2long(val)
VALUE val;
{
+ again:
if (NIL_P(val)) {
rb_raise(rb_eTypeError, "no implicit conversion from nil to integer");
}
@@ -1564,7 +1565,7 @@ rb_num2long(val)
default:
val = rb_to_int(val);
- return NUM2LONG(val);
+ goto again;
}
}
@@ -2531,6 +2532,16 @@ fix_rev(num)
return LONG2NUM(val);
}
+static VALUE
+fix_coerce(x)
+ VALUE x;
+{
+ while (!FIXNUM_P(x) && TYPE(x) != T_BIGNUM) {
+ x = rb_to_int(x);
+ }
+ return x;
+}
+
/*
* call-seq:
* fix & other => integer
@@ -2544,10 +2555,10 @@ fix_and(x, y)
{
long val;
- if (TYPE(y) == T_BIGNUM) {
+ if (!FIXNUM_P(y = fix_coerce(y))) {
return rb_big_and(y, x);
}
- val = FIX2LONG(x) & NUM2LONG(y);
+ val = FIX2LONG(x) & FIX2LONG(y);
return LONG2NUM(val);
}
@@ -2564,10 +2575,10 @@ fix_or(x, y)
{
long val;
- if (TYPE(y) == T_BIGNUM) {
+ if (!FIXNUM_P(y = fix_coerce(y))) {
return rb_big_or(y, x);
}
- val = FIX2LONG(x) | NUM2LONG(y);
+ val = FIX2LONG(x) | FIX2LONG(y);
return LONG2NUM(val);
}
@@ -2584,10 +2595,10 @@ fix_xor(x, y)
{
long val;
- if (TYPE(y) == T_BIGNUM) {
+ if (!FIXNUM_P(y = fix_coerce(y))) {
return rb_big_xor(y, x);
}
- val = FIX2LONG(x) ^ NUM2LONG(y);
+ val = FIX2LONG(x) ^ FIX2LONG(y);
return LONG2NUM(val);
}
@@ -2686,7 +2697,7 @@ fix_aref(fix, idx)
long val = FIX2LONG(fix);
long i;
- if (TYPE(idx) == T_BIGNUM) {
+ if (!FIXNUM_P(idx = fix_coerce(idx))) {
idx = rb_big_norm(idx);
if (!FIXNUM_P(idx)) {
if (!RBIGNUM(idx)->sign || val >= 0)
@@ -2694,7 +2705,7 @@ fix_aref(fix, idx)
return INT2FIX(1);
}
}
- i = NUM2LONG(idx);
+ i = FIX2LONG(idx);
if (i < 0) return INT2FIX(0);
if (sizeof(VALUE)*CHAR_BIT-1 < i) {