There’s one reason why this may not be a standard method:
there are at least two equally reasonable representations of
a mapping stored in an array:Nested arrays as returned by Hash#to_a: [[key1, val1], [key2, val2]]
A sequence: [key1, val1, key2, val2] as used by Hash
Obviously there is no simple way from a Hash to an Array and
back to a Hash.
(Btw: is that ever needed? I mean, if you have a hash you can
easily create a copy without an intermediate array.)So the question is wich representation Enumerable#to_h should
expect - nested or sequence?
I am concerned more on the language.
Which would you prefer?
Hash#to_a or Hash[*mumblearray.flatten]
IMHO, I prefer the first; more rubyish. The second looks weird, and my brain
aches just to remember it.
Now, since the first notation returns an array of pairs, then it is in
consonance with the hash notation (since hashes are pairs too). Ergo, to
answer your question, I choose the nested.
Ok, now I have a question for you robert (since you’re one of the ruby gurus
here), Is it possible for #to_h to determine if the array is nested or not,
and then adjust accordingly?
robert
kind regards -botp
···
Robert Klemme [mailto:bob.news@gmx.net] wrote: