t = Struct.new('Topic', :title, :replies)
t1 = t.new('first topic', ['aaa','bbb','ccc'])
t2 = t.new('second topic', ['ddd','eee','fff'])
topics = [t1, t2]
topics.map! do |topic|
topic.replies.map! do |reply|
reply * 2
end
topic
end
puts topics
不用重新赋值,直接修改,很直观,处理回帖内容的时候经常用到
PS:ruby中map是collect的一个别名