mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* array.c (rb_ary_shuffle_bang, rb_ary_sample): check
unknown keywords. * test/ruby/test_array.rb (test_shuffle, test_sample): tests for the above. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44064 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
66a96c13f9
commit
3e1360f370
3 changed files with 39 additions and 2 deletions
|
@ -1,3 +1,11 @@
|
||||||
|
Sun Dec 8 13:59:38 2013 Kazuki Tsujimoto <kazuki@callcc.net>
|
||||||
|
|
||||||
|
* array.c (rb_ary_shuffle_bang, rb_ary_sample): check
|
||||||
|
unknown keywords.
|
||||||
|
|
||||||
|
* test/ruby/test_array.rb (test_shuffle, test_sample): tests for
|
||||||
|
the above.
|
||||||
|
|
||||||
Sun Dec 8 13:01:11 2013 Aman Gupta <ruby@tmm1.net>
|
Sun Dec 8 13:01:11 2013 Aman Gupta <ruby@tmm1.net>
|
||||||
|
|
||||||
* vm.c (ruby_vm_stat): add RubyVM.stat() for access to internal cache
|
* vm.c (ruby_vm_stat): add RubyVM.stat() for access to internal cache
|
||||||
|
|
22
array.c
22
array.c
|
@ -4431,9 +4431,18 @@ rb_ary_shuffle_bang(int argc, VALUE *argv, VALUE ary)
|
||||||
{
|
{
|
||||||
VALUE opts, randgen = rb_cRandom;
|
VALUE opts, randgen = rb_cRandom;
|
||||||
long i, len;
|
long i, len;
|
||||||
|
static ID keyword_ids[1];
|
||||||
|
|
||||||
|
if (!keyword_ids[0]) {
|
||||||
|
keyword_ids[0] = rb_intern("random");
|
||||||
|
}
|
||||||
|
|
||||||
if (OPTHASH_GIVEN_P(opts)) {
|
if (OPTHASH_GIVEN_P(opts)) {
|
||||||
randgen = rb_hash_lookup2(opts, sym_random, randgen);
|
VALUE random;
|
||||||
|
rb_get_kwargs(opts, keyword_ids, 0, 1, &random);
|
||||||
|
if (random != Qundef) {
|
||||||
|
randgen = random;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
rb_check_arity(argc, 0, 0);
|
rb_check_arity(argc, 0, 0);
|
||||||
rb_ary_modify(ary);
|
rb_ary_modify(ary);
|
||||||
|
@ -4509,9 +4518,18 @@ rb_ary_sample(int argc, VALUE *argv, VALUE ary)
|
||||||
VALUE opts, randgen = rb_cRandom;
|
VALUE opts, randgen = rb_cRandom;
|
||||||
long n, len, i, j, k, idx[10];
|
long n, len, i, j, k, idx[10];
|
||||||
long rnds[numberof(idx)];
|
long rnds[numberof(idx)];
|
||||||
|
static ID keyword_ids[1];
|
||||||
|
|
||||||
|
if (!keyword_ids[0]) {
|
||||||
|
keyword_ids[0] = rb_intern("random");
|
||||||
|
}
|
||||||
|
|
||||||
if (OPTHASH_GIVEN_P(opts)) {
|
if (OPTHASH_GIVEN_P(opts)) {
|
||||||
randgen = rb_hash_lookup2(opts, sym_random, randgen);
|
VALUE random;
|
||||||
|
rb_get_kwargs(opts, keyword_ids, 0, 1, &random);
|
||||||
|
if (random != Qundef) {
|
||||||
|
randgen = random;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
len = RARRAY_LEN(ary);
|
len = RARRAY_LEN(ary);
|
||||||
if (argc == 0) {
|
if (argc == 0) {
|
||||||
|
|
|
@ -2085,6 +2085,13 @@ class TestArray < Test::Unit::TestCase
|
||||||
100.times do
|
100.times do
|
||||||
assert_equal([0, 1, 2].shuffle, [0, 1, 2].shuffle(random: gen))
|
assert_equal([0, 1, 2].shuffle, [0, 1, 2].shuffle(random: gen))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
assert_raise_with_message(ArgumentError, /unknown keyword/) do
|
||||||
|
[0, 1, 2].shuffle(xawqij: "a")
|
||||||
|
end
|
||||||
|
assert_raise_with_message(ArgumentError, /unknown keyword/) do
|
||||||
|
[0, 1, 2].shuffle!(xawqij: "a")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_shuffle_random
|
def test_shuffle_random
|
||||||
|
@ -2158,6 +2165,10 @@ class TestArray < Test::Unit::TestCase
|
||||||
assert_equal(a.sample(n), a.sample(n, random: gen), "#{i}/#{n}")
|
assert_equal(a.sample(n), a.sample(n, random: gen), "#{i}/#{n}")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
assert_raise_with_message(ArgumentError, /unknown keyword/) do
|
||||||
|
[0, 1, 2].sample(xawqij: "a")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_sample_random
|
def test_sample_random
|
||||||
|
|
Loading…
Add table
Reference in a new issue