Попробуйте это:
def extract_list(hash, collect = false)
hash.map do |k, v|
v.is_a?(Hash) ? extract_list(v, (k == "items_attributes")) :
(collect ? v : nil)
end.compact.flatten
end
Теперь давайте проверим функцию:
>> input = {
'category_attributes' => {
'name' => "Gardening",
'items_attributes' => {
'item 1' => "backhoe",
'item 2' => "whellbarrel",
'children_attributes' => {
'name' => "seeds",
'items_attributes' => {
'item 3' => "various flower seeds",
'item 4' => "various tree seeds"
},
'children_attributes' => {
'name' => "agricultural seeds",
'items_attributes' => {
'item 5' => "corn",
'item 6' => "wheat"
}
}
}
}
}
}
>> extract_list(input)
=> ["various flower seeds", "various tree seeds", "wheat", "corn",
"backhoe", "whellbarrel"]