From: Akinori MUSHA Date: 2008-04-10T23:48:54+09:00 Subject: [ruby-dev:34313] Enumerable#find_index vs. Array#index --pgp-sign-Multipart_Thu_Apr_10_23:51:14_2008-1 Content-Type: text/plain; charset=ISO-2022-JP  [ruby-talk:178495] が発端で Enumerable#find_index というのが 以前追加されましたが、機能的には Array#index のサブセットです。 (index はブロック評価、指定値との比較を両サポート)  Array 以外では #index(value) に相当する機能がないので、 Enumerable#find_index(value) をサポートしてはどうでしょうか。 (そして Array では効率のため alias find_index index します) Index: enum.c =================================================================== --- enum.c (revision 15954) +++ enum.c (working copy) @@ -186,8 +186,19 @@ } static VALUE -find_index_i(VALUE i, VALUE *memo, int argc, VALUE *argv) +find_index_i(VALUE i, VALUE *memo) { + if (rb_equal(i, memo[2])) { + memo[0] = UINT2NUM(memo[1]); + rb_iter_break(); + } + memo[1]++; + return Qnil; +} + +static VALUE +find_index_iter_i(VALUE i, VALUE *memo, int argc, VALUE *argv) +{ if (RTEST(enum_yield(argc, argv))) { memo[0] = UINT2NUM(memo[1]); rb_iter_break(); @@ -198,30 +209,48 @@ /* * call-seq: - * enum.find_index() {| obj | block } => int + * enum.find_index(value) => int or nil + * enum.find_index {| obj | block } => int or nil * - * Passes each entry in enum to block. Returns the - * index for the first for which block is not false. - * If no object matches, returns nil + * Compares each entry in enum with value or passes + * to block. Returns the index for the first for which the + * evaluated value is non-false. If no object matches, returns + * nil * * (1..10).find_index {|i| i % 5 == 0 and i % 7 == 0 } #=> nil * (1..100).find_index {|i| i % 5 == 0 and i % 7 == 0 } #=> 34 + * (1..100).find_index(50) #=> 49 * */ static VALUE -enum_find_index(VALUE obj) +enum_find_index(int argc, VALUE *argv, VALUE obj) { - VALUE memo[2]; + VALUE memo[3]; /* [return value, current index, condition value] */ + rb_block_call_func *func; - RETURN_ENUMERATOR(obj, 0, 0); - memo[0] = Qundef; - memo[1] = 0; - rb_block_call(obj, id_each, 0, 0, find_index_i, (VALUE)memo); - if (memo[0] != Qundef) { - return memo[0]; + if (argc == 0) { + RETURN_ENUMERATOR(obj, 0, 0); + memo[0] = Qnil; + memo[1] = 0; + memo[2] = Qundef; + func = find_index_iter_i; } - return Qnil; + else { + VALUE item; + + if (rb_block_given_p()) { + rb_warn("given block not used"); + } + rb_scan_args(argc, argv, "1", &item); + memo[0] = Qnil; + memo[1] = 0; + memo[2] = item; + func = find_index_i; + } + + rb_block_call(obj, id_each, 0, 0, func, (VALUE)memo); + return memo[0]; } static VALUE @@ -1688,7 +1717,7 @@ rb_define_method(rb_mEnumerable,"count", enum_count, -1); rb_define_method(rb_mEnumerable,"find", enum_find, -1); rb_define_method(rb_mEnumerable,"detect", enum_find, -1); - rb_define_method(rb_mEnumerable,"find_index", enum_find_index, 0); + rb_define_method(rb_mEnumerable,"find_index", enum_find_index, -1); rb_define_method(rb_mEnumerable,"find_all", enum_find_all, 0); rb_define_method(rb_mEnumerable,"select", enum_find_all, 0); rb_define_method(rb_mEnumerable,"reject", enum_reject, 0); Index: array.c =================================================================== --- array.c (revision 15954) +++ array.c (working copy) @@ -877,6 +877,8 @@ * a.index("b") #=> 1 * a.index("z") #=> nil * a.index{|x|x=="b"} #=> 1 + * + * This is an alias of #find_index. */ static VALUE @@ -3378,6 +3380,7 @@ rb_define_method(rb_cArray, "length", rb_ary_length, 0); rb_define_alias(rb_cArray, "size", "length"); rb_define_method(rb_cArray, "empty?", rb_ary_empty_p, 0); + rb_define_method(rb_cArray, "find_index", rb_ary_index, -1); rb_define_method(rb_cArray, "index", rb_ary_index, -1); rb_define_method(rb_cArray, "rindex", rb_ary_rindex, -1); rb_define_method(rb_cArray, "join", rb_ary_join_m, -1); -- Akinori MUSHA / http://akinori.org/ --pgp-sign-Multipart_Thu_Apr_10_23:51:14_2008-1 Content-Type: application/pgp-signature Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (FreeBSD) iD8DBQBH/ilikgvvx5/Z4e4RAv7WAJ9lO8b/g3RK/okD010yvnQBWdozWwCgwBYz uDIB+v40yc+PN/vIdwxX4ao= =WppB -----END PGP SIGNATURE----- --pgp-sign-Multipart_Thu_Apr_10_23:51:14_2008-1--