1
使用下面的代碼行,我試圖插入一個元組到@test_results
陣列的@test_object
的:語法錯誤,意想不到的TIVAR,期待「(」
@[email protected]_results << [@u, @m, @r, @p]
但它引發以下錯誤:
unexpected tIVAR, expecting '(' (SyntaxError)
爲什麼Ruby的期待「(」?
使用下面的代碼行,我試圖插入一個元組到@test_results
陣列的@test_object
的:語法錯誤,意想不到的TIVAR,期待「(」
@[email protected]_results << [@u, @m, @r, @p]
但它引發以下錯誤:
unexpected tIVAR, expecting '(' (SyntaxError)
爲什麼Ruby的期待「(」?
的問題是,爲什麼你鍵入[email protected]_results
?這不是從O訪問對象的實例變量的正確方法物體的邊緣。這就是爲什麼你有這個錯誤。
你或許應該在課堂訪問該@test_object
屬於:
attr_accessor :test_results
或只是一個讀者,如果你不需要test_results=
方法:
attr_reader :test_results
前者相當於:
def test_results
@test_results
end
def test_results=(value)
@test_results = value
end
後者相當於:
def test_results
@test_results
end
然後,您可以簡單地輸入:
@test_object.test_results << [@u, @m, @r, @p]
我把「@」,因爲我得到了一個錯誤,否則,但我完全忘了在測試對象的類attr_accessor中,我是假設我有一個。 – blueseal