Each key in a hash has a value that’s also a hash.
{
100 => {
1 => 'ruby',
2 => 'enumerables'
},
50 => {
3 => 'can',
4 => 'cause'
},
15 => {
5 => 'occassional',
6 => 'insanity'
}
}
For each hash object, I want to discard the top-level key, and replace it with the key and value of the nested hash objects.
{
1 => 'ruby',
2 => 'enumerables',
3 => 'can',
4 => 'cause',
5 => 'occasional',
6 => 'insanity'
}
I have it working, but my method uses a merge!, and requires creating another hash to store the values. I’m curious to see if it can be done in one line. I tried to use reduce(), but could not make it work.
This works:
Edit: Another option, using
reduce(which is the same asinject), and noting tokland’s comment that to_proc is automatically called when you use a symbol:Then it becomes not only concise but very readable.