码迷,mamicode.com
首页 > 其他好文 > 详细

map

时间:2016-04-30 18:15:09      阅读:143      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!