diff options
author | Benoit Daloze <[email protected]> | 2020-12-14 20:24:18 +0100 |
---|---|---|
committer | Benoit Daloze <[email protected]> | 2020-12-14 20:29:50 +0100 |
commit | f5c89c1660afd3a89514125aad579c0a96990c4b (patch) | |
tree | 119a4e2441a87c9d70870c0d5ad97e8f00f7869f | |
parent | c183288754fdad17e627c4182de599d965e99405 (diff) |
Deprecate Random::DEFAULT
* Closes [Feature #17351].
-rw-r--r-- | NEWS.md | 13 | ||||
-rw-r--r-- | random.c | 2 | ||||
-rw-r--r-- | spec/ruby/core/random/default_spec.rb | 18 | ||||
-rw-r--r-- | test/ruby/test_rand.rb | 2 |
4 files changed, 32 insertions, 3 deletions
@@ -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 @@ -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) |