aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--NEWS.md13
-rw-r--r--random.c2
-rw-r--r--spec/ruby/core/random/default_spec.rb18
-rw-r--r--test/ruby/test_rand.rb2
4 files changed, 32 insertions, 3 deletions
diff --git a/NEWS.md b/NEWS.md
index 1f4e8915cb..501cab351a 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -269,6 +269,17 @@ Outstanding ones only.
* 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
[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 9d33e21d11..d05f3f77c2 100644
--- a/random.c
+++ b/random.c
@@ -61,6 +61,7 @@
#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)
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 014cc378a9..a709eddd53 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'
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 af10a27ce0..13b7329269 100644
--- a/test/ruby/test_rand.rb
+++ b/test/ruby/test_rand.rb
@@ -394,8 +394,10 @@ class TestRand < Test::Unit::TestCase
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)