语法错误,意外的tIVAR,期待’(’

使用以下代码行,我试图将一个元组插入@test_results数组中:

 @test_object.@test_results << [@u, @m, @r, @p] 

但它引发了以下错误:

 unexpected tIVAR, expecting '(' (SyntaxError) 

为什么Ruby期待’(’?

问题是,为什么要输入.@test_results ? 这不是从对象外部访问对象的实例变量的正确方法。 这就是你有这个错误的原因。

您可能应该在@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]