2017-05-10 144 views
-2

我的Ruby代碼...未初始化的常量Person_instance(NameError)

class Person 
    attr_accessor :name, :age, :gender 
    person_instance = Person.new 
    person_instance.name = "Konstantin" 
    person_instance.age = 19 
    person_instance.gender = "male" 
end 
puts person_instance.age 

我的錯誤在Ruby中......

C:/Users/Balu/RubymineProjects/learning/test:10:in '<top (required)>': uninitialized constant Person_instance (NameError) 
from -e:1:in 'load' 
from -e:1:in '<main>' 

過程與exit code 1完成。

+0

局部變量不能在課堂外訪問,你想要做什麼? –

回答

2

使用它通過以下方式

class Person 
    attr_accessor :name, :age, :gender 
end 
person_instance = Person.new 
person_instance.name = "Konstantin" 
person_instance.age = 19 
person_instance.gender = "male" 
puts person_instance.age 

你基本上需要聲明的情況下,一旦你的類被聲明。

相關問題