From 9ba8db842e7bbf6f08ce44931c89abccf0472ede Mon Sep 17 00:00:00 2001 From: knu Date: Tue, 12 Aug 2008 05:56:10 +0000 Subject: * 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 --- array.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'array.c') diff --git a/array.c b/array.c index 9bd5e0bff3..5e2ed4db92 100644 --- a/array.c +++ b/array.c @@ -3349,6 +3349,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 } @@ -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); -- cgit v1.2.3