新手问题 有关 Array.new 的问题

vowxie · 2015年09月22日 · 最后由 vowxie 回复于 2015年09月22日 · 1156 次阅读

因为想练习列表的一些操作,所以随手在 Google 上搜索了随机生成数字数组的方法

找到了一行代码,想不通为什么可以这么写

Array.new(20){rand(30)} 

In the last form, an array of the given size is created. Each element in this array is created by passing the element’s index to the given block and storing the return value.

http://ruby-doc.org/core-2.2.0/Array.html#method-c-new

#1 楼 @rei 多谢,我指的是我完全没想到查文档。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号