From: Yusuke ENDOH Date: 2008-02-05T22:00:45+09:00 Subject: [ruby-dev:33603] Hash#flatten flattens recursively 遠藤と申します。 Hash#flatten のコメントを読むと、このメソッドはデフォルトでは 再帰的に flatten しないと書いてあるのですが、実際にはするようです。 y$ ./ruby -ve 'p({1 => "one", 2 => [2,"two"], 3 => "three"}.flatten)' ruby 1.9.0 (2008-02-05 revision 15377) [i686-linux] [1, "one", 2, 2, "two", 3, "three"] コメントと実装のどちらが正しいのでしょうか。 以下は Hash#flatten のコメントの引用です。 > /* > * call-seq: > * hash.flatten -> an_array > * hash.flatten(level) -> an_array > * > * Returns a new array that is a one-dimensional flattening of this > * hash. That is, for every key or value that is an array, extract > * its elements into the new array. Unlike Array#flatten, this > * method does not flatten recursively by default. If the optional > * level argument determines the level of recursion to flatten. > * > * a = {1=> "one", 2 => [2,"two"], 3 => "three"} > * a.flatten # => [1, "one", 2, [2, "two"], 3, "three"] > * a.flatten(2) # => [1, "one", 2, 2, "two", 3, "three"] > */ -- Yusuke ENDOH