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}」}' –