您的位置:首页 > 编程语言 > Ruby

using an enumerable built-in Ruby to access and manipulate nested data

2015-04-27 10:29 776 查看
array = [5, 10, [15, 20], 25, [30, 35, 40]
array#method { #block that adds 5} => [10, 15, [20, 25], 30, [35, 40, 45]
You could use a recursive lambda:
add_five = lambda { |e| e.is_a?(Enumerable) ? e.map(&add_five) : e + 5 }new_array = array.map(&add_five)
Adjust the 
e.is_a?(Enumerable)
 test to match your situation, 
e.is_a?(Array)
 would be tighter but possibly unnecessarily so.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐