1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Deprecate Random::DEFAULT

* Closes [Feature #17351].
This commit is contained in:
Benoit Daloze 2020-12-14 20:24:18 +01:00
parent c183288754
commit f5c89c1660
4 changed files with 32 additions and 3 deletions

13
NEWS.md
View file

@ -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

View file

@ -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);

View file

@ -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

View file

@ -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)