y=(1..10).map{|i| i.to_s==$*[0]?($*.shift;?0):?.}
4.times{|i| puts (y.pop(4-i)* ' ').center 8 }
输出:
. . . .
. . .
. .
.
parens内部的* ' '
是什么
答案 0 :(得分:1)
在第一行之后,y
是一个像这样的字符串数组:
y=(1..10).map{|i| i.to_s==$*[0]?($*.shift;?0):?.}
# => [".", ".", ".", ".", ".", ".", ".", ".", ".", "."]
Array#*str
相当于Array#join(str)
,它将字符串连接在一起。特别是,* ' '
将数组中的字符串与空格连接起来:
y.pop(3)
# => [".", ".", "."]
y.pop(3) * ' '
# => ". . ."