| 赞 | 15 |
| VIP | |
| 好人卡 | |
| 积分 | 3 |
| 经验 | |
| 最后登录 | 2022-8-25 |
| 在线时间 | 2355 小时 |
- 梦石
- 0
- 星屑
- 266
- 在线时间
- 2355 小时
- 注册时间
- 2009-3-13
- 回帖
- 2257

|
发表于 2013-2-21 10:54:11
|
显示全部楼层
本帖最后由 Sion 于 2013-2-21 13:07 编辑
Array[i, n]就是得到Array从索引 i 处开始之后的 n 个元素(包含元素 i )组成的数组- a = ["a", "b", "c", "d", "e", "f"]
- i = 0; n = 4
- p a[i, n] #=> ["a", "b", "c", "d"]
- p a #=> ["a", "b", "c", "d", "e", "f"]
复制代码 each_index是索引;each_with_index是元素+索引,block可以设置2个参数,只用1个参数的时候等于each:- a = ["a", "b", "c"]
- a.each_index {|i| print i, " -- " } #=> 0 -- 1 -- 2 --
- puts ""
- a.each_with_index {|n| print n, " -- " } #=> a -- b -- c --
- puts ""
- a.each {|n| print n, " -- " } #=> a -- b -- c --
- puts ""
- a.each_with_index {|n,i| print n, ",", i, " -- " } #=> a,0 -- b,1 -- c,2 --
复制代码 |
评分
-
查看全部评分
|