diff options
author | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-08-12 05:56:10 +0000 |
---|---|---|
committer | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-08-12 05:56:10 +0000 |
commit | 9ba8db842e7bbf6f08ce44931c89abccf0472ede (patch) | |
tree | 08f8962e886c3da6f75770c862b32a02423fc300 /array.c | |
parent | e65c07e98b1b076ab99a2b12043148b3b4962386 (diff) |
* array.c (rb_ary_choice): Resurrect #choice. Let me think about
it for a while.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@18514 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r-- | array.c | 22 |
1 files changed, 22 insertions, 0 deletions
@@ -3351,6 +3351,27 @@ rb_ary_sample(argc, argv, ary) /* * call-seq: + * array.choice -> obj + * + * Choose a random element from an array. NOTE: This method will be + * deprecated in future. Use #sample instead. + */ + +static VALUE +rb_ary_choice(ary) + VALUE ary; +{ + long i, j; + + i = RARRAY(ary)->len; + if (i == 0) return Qnil; + j = rb_genrand_real()*i; + return RARRAY(ary)->ptr[j]; +} + + +/* + * call-seq: * ary.cycle {|obj| block } * ary.cycle(n) {|obj| block } * @@ -3882,6 +3903,7 @@ Init_Array() rb_define_method(rb_cArray, "shuffle!", rb_ary_shuffle_bang, 0); rb_define_method(rb_cArray, "shuffle", rb_ary_shuffle, 0); rb_define_method(rb_cArray, "sample", rb_ary_sample, -1); + rb_define_method(rb_cArray, "choice", rb_ary_choice, 0); rb_define_method(rb_cArray, "cycle", rb_ary_cycle, -1); rb_define_method(rb_cArray, "permutation", rb_ary_permutation, -1); rb_define_method(rb_cArray, "combination", rb_ary_combination, 1); |