Skip to content

Commit

Permalink
* array.c (rb_ary_shuffle_bang, rb_ary_sample): check
Browse files Browse the repository at this point in the history
  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
  • Loading branch information
k-tsj committed Dec 8, 2013
1 parent 66a96c1 commit 3e1360f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
8 changes: 8 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
Sun Dec 8 13:59:38 2013 Kazuki Tsujimoto <[email protected]>

* 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 <[email protected]>

* vm.c (ruby_vm_stat): add RubyVM.stat() for access to internal cache
Expand Down
22 changes: 20 additions & 2 deletions array.c
Original file line number Diff line number Diff line change
Expand Up @@ -4431,9 +4431,18 @@ rb_ary_shuffle_bang(int argc, VALUE *argv, VALUE ary)
{
VALUE opts, randgen = rb_cRandom;
long i, len;
static ID keyword_ids[1];

if (!keyword_ids[0]) {
keyword_ids[0] = rb_intern("random");
}

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_ary_modify(ary);
Expand Down Expand Up @@ -4509,9 +4518,18 @@ rb_ary_sample(int argc, VALUE *argv, VALUE ary)
VALUE opts, randgen = rb_cRandom;
long n, len, i, j, k, idx[10];
long rnds[numberof(idx)];
static ID keyword_ids[1];

if (!keyword_ids[0]) {
keyword_ids[0] = rb_intern("random");
}

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);
if (argc == 0) {
Expand Down
11 changes: 11 additions & 0 deletions test/ruby/test_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2085,6 +2085,13 @@ def test_shuffle
100.times do
assert_equal([0, 1, 2].shuffle, [0, 1, 2].shuffle(random: gen))
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

def test_shuffle_random
Expand Down Expand Up @@ -2158,6 +2165,10 @@ def test_sample
assert_equal(a.sample(n), a.sample(n, random: gen), "#{i}/#{n}")
end
end

assert_raise_with_message(ArgumentError, /unknown keyword/) do
[0, 1, 2].sample(xawqij: "a")
end
end

def test_sample_random
Expand Down

0 comments on commit 3e1360f

Please sign in to comment.