ruby-changes:50381
From: nobu <ko1@a...>
Date: Tue, 20 Feb 2018 18:26:43 +0900 (JST)
Subject: [ruby-changes:50381] nobu:r62497 (trunk): random.c: Random.bytes
nobu 2018-02-20 18:26:38 +0900 (Tue, 20 Feb 2018) New Revision: 62497 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=62497 Log: random.c: Random.bytes * random.c (random_s_bytes): new method Random.bytes, which is equivalent to Random::DEFAULT.bytes. [Feature #4938] Modified files: trunk/NEWS trunk/random.c trunk/test/ruby/test_rand.rb Index: test/ruby/test_rand.rb =================================================================== --- test/ruby/test_rand.rb (revision 62496) +++ test/ruby/test_rand.rb (revision 62497) @@ -347,10 +347,15 @@ END https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rand.rb#L347 end def assert_random_bytes(r) + srand(0) assert_equal("", r.bytes(0)) - assert_equal("\xAC".force_encoding("ASCII-8BIT"), r.bytes(1)) - assert_equal("/\xAA\xC4\x97u\xA6\x16\xB7\xC0\xCC".force_encoding("ASCII-8BIT"), - r.bytes(10)) + assert_equal("", Random.bytes(0)) + x = "\xAC".force_encoding("ASCII-8BIT") + assert_equal(x, r.bytes(1)) + assert_equal(x, Random.bytes(1)) + x = "/\xAA\xC4\x97u\xA6\x16\xB7\xC0\xCC".force_encoding("ASCII-8BIT") + assert_equal(x, r.bytes(10)) + assert_equal(x, Random.bytes(10)) end def test_random_range Index: random.c =================================================================== --- random.c (revision 62496) +++ random.c (revision 62497) @@ -1134,6 +1134,19 @@ rb_random_bytes(VALUE obj, long n) https://github.com/ruby/ruby/blob/trunk/random.c#L1134 return genrand_bytes(rnd, n); } +/* + * call-seq: Random.bytes(size) -> a_string + * + * Returns a random binary string. The argument size specified the length of + * the result string. + */ +static VALUE +random_s_bytes(VALUE obj, VALUE len) +{ + rb_random_t *rnd = rand_start(&default_rand); + return genrand_bytes(rnd, NUM2LONG(rb_to_int(len))); +} + static VALUE range_values(VALUE vmax, VALUE *begp, VALUE *endp, int *exclp) { @@ -1636,6 +1649,7 @@ InitVM_Random(void) https://github.com/ruby/ruby/blob/trunk/random.c#L1649 rb_define_singleton_method(rb_cRandom, "srand", rb_f_srand, -1); rb_define_singleton_method(rb_cRandom, "rand", random_s_rand, -1); + rb_define_singleton_method(rb_cRandom, "bytes", random_s_bytes, 1); rb_define_singleton_method(rb_cRandom, "new_seed", random_seed, 0); rb_define_singleton_method(rb_cRandom, "urandom", random_raw_seed, 1); rb_define_private_method(CLASS_OF(rb_cRandom), "state", random_s_state, 0); Index: NEWS =================================================================== --- NEWS (revision 62496) +++ NEWS (revision 62497) @@ -34,6 +34,12 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L34 * Proc#call doesn't change $SAFE any more. [Feature #14250] +* Random + + * New methods: + + * added Random.bytes. [Feature #4938] + === Stdlib updates (outstanding ones only) * Matrix -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/