语法错误,未预期的tIVAR,预期为'(' [英] Syntax error, unexpected tIVAR, expecting '('
本文介绍了语法错误,未预期的tIVAR,预期为'('的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用下面的代码行,我试图在@test_object
的@test_results
数组中插入一个元组:
Using the following line of code I'm trying to insert a tuple into the @test_results
array of the @test_object
:
@test_object.@test_results << [@u, @m, @r, @p]
但是它会引发以下错误:
But it raise the following error:
unexpected tIVAR, expecting '(' (SyntaxError)
为什么露比会期望'('?
Why is Ruby expecting '('?
推荐答案
问题是,为什么要键入.@test_results
?这不是从对象外部访问对象的实例变量的正确方法.这就是为什么您会遇到此错误.
The question is, why do you type .@test_results
? It's not the proper way to access object's instance variable from outside of the object. That's why you have this error.
您可能应该在@test_object
所属的类中具有访问器:
You probably should have accessor in class that @test_object
belongs to:
attr_accessor :test_results
或只是一个阅读器,如果您不需要test_results=
方法:
or just a reader, if you don't need test_results=
method:
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]
这篇关于语法错误,未预期的tIVAR,预期为'('的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文