From 9ba8db842e7bbf6f08ce44931c89abccf0472ede Mon Sep 17 00:00:00 2001 From: knu Date: Tue, 12 Aug 2008 05:56:10 +0000 Subject: [PATCH] * 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 --- ChangeLog | 5 +++++ array.c | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/ChangeLog b/ChangeLog index 00d7fe3c59..e6296041a1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Tue Aug 12 14:53:33 2008 Akinori MUSHA + + * array.c (rb_ary_choice): Resurrect #choice. Let me think about + it for a while. + Tue Aug 12 13:57:11 2008 Akinori MUSHA * misc/ruby-mode.el (ruby-imenu-create-index-in-block): Fix the 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);