From: Yusuke ENDOH Date: 2007-11-06T23:44:11+09:00 Subject: [ruby-dev:32181] each_with_index yields mutable array 遠藤と申します。 1.9 で Enumearble#each_with_index が yield してくる配列が変更可能です。 無駄に便利かもしれないのですが、仕様でしょうか。 $ ./ruby -e '%w(a b c).each_with_index {|r| p r; r.push r[0] }' ["a", 0] ["b", 1, "a"] ["c", 2, "a", "b"] この配列を縮めた場合、理論上アクセス違反する可能性があるような気がします。 (現実的には発症しないと思いますが) $ ./ruby -e '%w(a b c).each_with_index {|r| p r; r.pop }' ["a", 0] ["b"] [] とりあえずこの配列を freeze させるパッチです。 Index: enum.c =================================================================== --- enum.c (revision 13827) +++ enum.c (working copy) @@ -1327,6 +1327,7 @@ RETURN_ENUMERATOR(obj, argc, argv); memo = rb_ary_new3(2, Qnil, INT2FIX(0)); + rb_ary_freeze(memo); rb_block_call(obj, id_each, argc, argv, each_with_index_i, memo); return obj; } -- Yusuke ENDOH