out_file = File.open "out_file.txt" , 'w' do |f|
matrix = [
[1,2,3],
[4,5,6],
[7,8,9]
]
f.puts "matrix test"
f.puts " int at [0,2]: #{matrix[0][2]}"
f.puts " int at [2,0]: #{matrix[2][0]}"
f.puts " int at {1,1]: #{matrix[1][1]}"
以上代碼生成這樣的:紅寶石陣列,[]操作
"matrix test
int at [0,2]: 3
int at [2,0]: 7
int at {1,1]: 5"
但是這個代碼使用相同的矩陣變量聲明..
rows = Array(0..3)
cols = Array(0..3)
rows.each do |r|
cols.each do |c|
f.puts "row:#{r} col:#{c} = #{matrix[r][c]},"
end
end
產生一個錯誤:
undefined method `[]' for nil:NilClass (NoMethodError)
有人可以告訴我什麼是g在嗎?
許多THX。我確實有雙點和三點算子混淆。 – warwick
@warwick - 很高興聽到它的幫助。您可能想投票並接受最能夠幫助您的答案(帶有複選標記),以便與您有類似問題的其他人可以快速找到最佳答案。這也有助於您的聲譽。 –