0
我有一个数组建立像如何正确地将数组元素分离为字符串?
%w(Dog Cat Bird Rat).each_with_index do |element, index|
# "w" for word array
# It's a shortcut for arrays
puts ("%-4s + #{index}" % element)
end
这将输出类似于像一些
Dog + 0
Cat + 1
Bird + 2
Rat + 3
如果我想改变动物的东西,如一个字符串? 所以它说
This is string 0 + 0
This is string 1 + 1
This is string 2 + 2
etc
有没有办法做到这一点? 这不起作用:
%w('This is string 0', 'This is string 1', 'This is string 2', 'This is string 3').each_with_index do |element, index|
# "w" for word array
# It's a shortcut for arrays
puts ("%-4s + #{index}" % element)
end
'4.times {|我|放入“这是字符串#{i} +#{i}”}' –