diff --git a/compile.c b/compile.c index 341361dc78..d9894d27d8 100644 --- a/compile.c +++ b/compile.c @@ -2005,10 +2005,15 @@ cdhash_cmp(VALUE val, VALUE lit) else if (tlit == T_FLOAT) { return rb_float_cmp(lit, val); } - else if (tlit == T_RATIONAL) { - const struct RRational *dat1 = RRATIONAL(val); - const struct RRational *dat2 = RRATIONAL(lit); - return (dat1->num == dat2->num) && (dat1->den == dat2->den); + else if (tlit == T_RATIONAL) { + const struct RRational *rat1 = RRATIONAL(val); + const struct RRational *rat2 = RRATIONAL(lit); + return (rat1->num == rat2->num) && (rat1->den == rat2->den); + } + else if (tlit == T_COMPLEX) { + const struct RComplex *comp1 = RCOMPLEX(val); + const struct RComplex *comp2 = RCOMPLEX(lit); + return (comp1->real == comp2->real) && (comp1->imag == comp2->imag); } else { UNREACHABLE_RETURN(-1); @@ -2030,6 +2035,8 @@ cdhash_hash(VALUE a) return rb_dbl_long_hash(RFLOAT_VALUE(a)); case T_RATIONAL: return rb_rational_hash(a); + case T_COMPLEX: + return rb_complex_hash(a); default: UNREACHABLE_RETURN(0); } diff --git a/complex.c b/complex.c index fe8def28d1..66d9754331 100644 --- a/complex.c +++ b/complex.c @@ -1326,8 +1326,8 @@ nucomp_numerator(VALUE self) } /* :nodoc: */ -static VALUE -nucomp_hash(VALUE self) +st_index_t +rb_complex_hash(VALUE self) { st_index_t v, h[2]; VALUE n; @@ -1338,7 +1338,13 @@ nucomp_hash(VALUE self) n = rb_hash(dat->imag); h[1] = NUM2LONG(n); v = rb_memhash(h, sizeof(h)); - return ST2FIX(v); + return v; +} + +static VALUE +nucomp_hash(VALUE self) +{ + return ST2FIX(rb_complex_hash(self)); } /* :nodoc: */ diff --git a/internal/complex.h b/internal/complex.h index 6f63a9ebeb..9eae804ddb 100644 --- a/internal/complex.h +++ b/internal/complex.h @@ -25,5 +25,6 @@ struct RComplex { /* complex.c */ VALUE rb_dbl_complex_new_polar_pi(double abs, double ang); +st_index_t rb_complex_hash(VALUE comp); #endif /* INTERNAL_COMPLEX_H */ diff --git a/test/ruby/test_rational.rb b/test/ruby/test_rational.rb index 5262b6edad..27d0527d42 100644 --- a/test/ruby/test_rational.rb +++ b/test/ruby/test_rational.rb @@ -839,6 +839,10 @@ class Rational_Test < Test::Unit::TestCase n = case 3/2r when 1.5r then true else false end assert_equal(n, true, '[ruby-core:103759] [Bug #17854]') RUBY + assert_separately([], <<-RUBY) + n = case 1i when 1i then true else false end + assert_equal(n, true, '[ruby-core:103759] [Bug #17854]') + RUBY end def test_Rational_with_invalid_exception