首页 > 其他 > 详细

map

时间:2016-04-30 18:13:55      阅读:228      评论:0      收藏:0      [点我收藏+]

 

map is another extremely useful Ruby iterator. It helps us map data from one form to another. It will return a new Array containing the result of running the specific block on each element in the collection. Let’s start with a simple example using an Array of numbers:

nums = [1,2,3,4,5]

 

If we wanted a new Array where every item is double the original item in nums:

double_arr = nums.map {|num| num * 2}

 

map will run num*2 on every item in the nums Array, storing the result in a new Array. That new Array looks like this:

[2, 4, 6, 8, 10]

 

map

原文:http://www.cnblogs.com/iwangzheng/p/5449016.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!