[前][次][番号順一覧][スレッド一覧]

ruby-changes:31985

From: ktsj <ko1@a...>
Date: Sun, 8 Dec 2013 14:02:19 +0900 (JST)
Subject: [ruby-changes:31985] ktsj:r44064 (trunk): * array.c (rb_ary_shuffle_bang, rb_ary_sample): check

ktsj	2013-12-08 14:02:12 +0900 (Sun, 08 Dec 2013)

  New Revision: 44064

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=44064

  Log:
    * 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.

  Modified files:
    trunk/ChangeLog
    trunk/array.c
    trunk/test/ruby/test_array.rb
Index: array.c
===================================================================
--- array.c	(revision 44063)
+++ array.c	(revision 44064)
@@ -4431,9 +4431,18 @@ rb_ary_shuffle_bang(int argc, VALUE *arg https://github.com/ruby/ruby/blob/trunk/array.c#L4431
 {
     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);
@@ -4509,9 +4518,18 @@ rb_ary_sample(int argc, VALUE *argv, VAL https://github.com/ruby/ruby/blob/trunk/array.c#L4518
     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) {
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 44063)
+++ ChangeLog	(revision 44064)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Dec  8 13:59:38 2013  Kazuki Tsujimoto  <kazuki@c...>
+
+	* 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@t...>
 
 	* vm.c (ruby_vm_stat): add RubyVM.stat() for access to internal cache
Index: test/ruby/test_array.rb
===================================================================
--- test/ruby/test_array.rb	(revision 44063)
+++ test/ruby/test_array.rb	(revision 44064)
@@ -2085,6 +2085,13 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L2085
     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
@@ -2158,6 +2165,10 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L2165
         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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]