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

ruby-changes:64164

From: Benoit <ko1@a...>
Date: Tue, 15 Dec 2020 04:31:12 +0900 (JST)
Subject: [ruby-changes:64164] f5c89c1660 (master): Deprecate Random::DEFAULT

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

From f5c89c1660afd3a89514125aad579c0a96990c4b Mon Sep 17 00:00:00 2001
From: Benoit Daloze <eregontp@g...>
Date: Mon, 14 Dec 2020 20:24:18 +0100
Subject: Deprecate Random::DEFAULT

* Closes [Feature #17351].

diff --git a/NEWS.md b/NEWS.md
index 1f4e891..501cab3 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -269,6 +269,17 @@ Outstanding ones only. https://github.com/ruby/ruby/blob/trunk/NEWS.md#L269
     * New class added to enable parallel execution. See doc/ractor.md for
       more details.
 
+* Random
+
+    * `Random::DEFAULT` now refers to the `Random` class instead of being a `Random` instance,
+      so it can work with `Ractor`.
+      [[Feature #17322]]
+
+    * `Random::DEFAULT` is deprecated since its value is now confusing and it is no longer global,
+      use `Kernel.rand`/`Random.rand` directly, or create a `Random` instance with `Random.new` instead.
+      [[Feature #17351]]
+
+
 * String
 
     * The following methods now return or yield String instances
@@ -652,5 +663,7 @@ end https://github.com/ruby/ruby/blob/trunk/NEWS.md#L663
 [Feature #17187]: https://bugs.ruby-lang.org/issues/17187
 [Bug #17221]:     https://bugs.ruby-lang.org/issues/17221
 [Feature #17260]: https://bugs.ruby-lang.org/issues/17260
+[Feature #17322]: https://bugs.ruby-lang.org/issues/17322
+[Feature #17351]: https://bugs.ruby-lang.org/issues/17351
 [Feature #17371]: https://bugs.ruby-lang.org/issues/17371
 [GH-2991]:        https://github.com/ruby/ruby/pull/2991
diff --git a/random.c b/random.c
index 9d33e21..d05f3f7 100644
--- a/random.c
+++ b/random.c
@@ -61,6 +61,7 @@ https://github.com/ruby/ruby/blob/trunk/random.c#L61
 #include "internal/numeric.h"
 #include "internal/random.h"
 #include "internal/sanitizers.h"
+#include "internal/variable.h"
 #include "ruby_atomic.h"
 #include "ruby/random.h"
 #include "ruby/ractor.h"
@@ -1716,6 +1717,7 @@ InitVM_Random(void) https://github.com/ruby/ruby/blob/trunk/random.c#L1717
     rb_define_method(rb_cRandom, "==", rand_mt_equal, 1);
 
     rb_define_const(rb_cRandom, "DEFAULT", rb_cRandom);
+    rb_deprecate_constant(rb_cRandom, "DEFAULT");
 
     rb_define_singleton_method(rb_cRandom, "srand", rb_f_srand, -1);
     rb_define_singleton_method(rb_cRandom, "rand", random_s_rand, -1);
diff --git a/spec/ruby/core/random/default_spec.rb b/spec/ruby/core/random/default_spec.rb
index 014cc37..a709edd 100644
--- a/spec/ruby/core/random/default_spec.rb
+++ b/spec/ruby/core/random/default_spec.rb
@@ -3,18 +3,30 @@ require_relative '../../spec_helper' https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/random/default_spec.rb#L3
 describe "Random::DEFAULT" do
 
   it "returns a random number generator" do
-    Random::DEFAULT.should respond_to(:rand)
+    suppress_warning do
+      Random::DEFAULT.should respond_to(:rand)
+    end
   end
 
   ruby_version_is ''...'3.0' do
     it "returns a Random instance" do
-      Random::DEFAULT.should be_an_instance_of(Random)
+      suppress_warning do
+        Random::DEFAULT.should be_an_instance_of(Random)
+      end
     end
   end
 
   ruby_version_is '3.0' do
     it "refers to the Random class" do
-      Random::DEFAULT.should.equal?(Random)
+      suppress_warning do
+        Random::DEFAULT.should.equal?(Random)
+      end
+    end
+
+    it "is deprecated" do
+      -> {
+        Random::DEFAULT.should.equal?(Random)
+      }.should complain(/constant Random::DEFAULT is deprecated/)
     end
   end
 
diff --git a/test/ruby/test_rand.rb b/test/ruby/test_rand.rb
index af10a27..13b7329 100644
--- a/test/ruby/test_rand.rb
+++ b/test/ruby/test_rand.rb
@@ -394,8 +394,10 @@ class TestRand < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rand.rb#L394
   def test_default_seed
     assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")
     begin;
+      verbose, $VERBOSE = $VERBOSE, nil
       seed = Random::DEFAULT::seed
       rand1 = Random::DEFAULT::rand
+      $VERBOSE = verbose
       rand2 = Random.new(seed).rand
       assert_equal(rand1, rand2)
 
-- 
cgit v0.10.2


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

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