使用以下代码行,我尝试将一个元组插入到 @test_object
的 @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]
关于ruby - 语法错误,意外的 tIVAR,应为 '(',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21625992/