[前][次][番号順一覧][スレッド一覧]

ruby-changes:66158

From: =E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3 <ko1@a...>
Date: Wed, 12 May 2021 10:31:09 +0900 (JST)
Subject: [ruby-changes:66158] d0e6c6e682 (master): cdhash_cmp: rational literals with fractions

https://git.ruby-lang.org/ruby.git/commit/?id=d0e6c6e682

From d0e6c6e682b9ba2b0309a5177933a0628e8ef316 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3?=
 <shyouhei@r...>
Date: Fri, 7 May 2021 11:07:11 +0900
Subject: cdhash_cmp: rational literals with fractions

Nobu kindly pointed out that rational literals can have fractions.
---
 compile.c                  | 5 +++--
 test/ruby/test_rational.rb | 4 ++++
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/compile.c b/compile.c
index 9001fd1..341361d 100644
--- a/compile.c
+++ b/compile.c
@@ -2006,8 +2006,9 @@ cdhash_cmp(VALUE val, VALUE lit) https://github.com/ruby/ruby/blob/trunk/compile.c#L2006
         return rb_float_cmp(lit, val);
     }
     else if (tlit ==  T_RATIONAL) {
-        /* Rational literals don't have fractions. */
-        return cdhash_cmp(val, rb_rational_num(lit));
+        const struct RRational *dat1 = RRATIONAL(val);
+        const struct RRational *dat2 = RRATIONAL(lit);
+        return (dat1->num == dat2->num) && (dat1->den == dat2->den);
     }
     else {
         UNREACHABLE_RETURN(-1);
diff --git a/test/ruby/test_rational.rb b/test/ruby/test_rational.rb
index d7f4961..5262b6e 100644
--- a/test/ruby/test_rational.rb
+++ b/test/ruby/test_rational.rb
@@ -835,6 +835,10 @@ class Rational_Test < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rational.rb#L835
       n = case 1 when 2r then false else true end
       assert_equal(n, true, '[ruby-core:103759] [Bug #17854]')
     RUBY
+    assert_separately([], <<-RUBY)
+      n = case 3/2r when 1.5r then true else false end
+      assert_equal(n, true, '[ruby-core:103759] [Bug #17854]')
+    RUBY
   end
 
   def test_Rational_with_invalid_exception
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]