["a", "b", "c", "d"]
变成[["a", 0], ["b", 1], ["c", 2], ["d", 3]]
我写了:
all = []
arra.each_with_index do |arr, index|
new_arr = [arr, index]
all << new_arr
end
觉得有更好的办法,挑战一下?
:s/\(\"\w\"\)/[\1, _ph_]_ph2_\r/g
:let i=0 | g/_ph_/s//\=i/ |let i+=1
:%s/_ph2_\n//g
打散 (splat) 运算符,会调用对象的 to_ary 方法转换成数组
The Ruby Programming Language ch4s5, 注册个免费帐号可以看 safariflow 的: http://www.safariflow.com/library/view/The+Ruby+Programming+Language/9780596516178/ch04s05.html#id3274652
常用法:
array = [head, *tail]
head, *tail = array
def f head, *tail
...
end
lambda do |head, *tail|
...
end
# 嵌套展开
proc do |((head, *middle, tail), *middle, tail)|
...
end
#23 楼 @bhuztez axis indicator http://en.wikipedia.org/wiki/APL_syntax_and_symbols#Operators_and_axis_indicator
0 < n < 1 上下结构 n = 1 左右结构 1 < n 转置后左右结构
#27 楼 @bhuztez 这是更详细的描述? http://www.dyalog.com/MasteringDyalogAPL/MasteringDyalogAPL.pdf Spe-5.2 - Fractional Axis