page 553, in Converting Hashes, lines 2-3 :
… If you want a single-dimensional array,
you can use flatten, which is hash.to_a.flatten. By default, flatten does not flatten values that
happen to be arrays …
I have checked that :
irb(main):125:0> hash = {a: 1, b: 2, c: [3, [55,66]], d: 4}
=> {:a=>1, :b=>2, :c=>[3, [55, 66]], :d=>4}
irb(main):126:0> hash.to_a.flatten
=> [:a, 1, :b, 2, :c, 3, 55, 66, :d, 4]
Then I was confused to read that by default flatten does not flatten …
As Array#flatten
behaves differently as Hash#flatten
, I suggest to replace flatten by Hash#flatten :
By default, Hash#flatten
does not flatten values that …