In ruby how to use class level local variable? (a ruby newbie's question)

后端 未结 4 1054
挽巷
挽巷 2020-12-17 20:34

So suppose I have this (not working):

class User
   description = \"I am User class variable\"
   def print
       puts description
   end
end
4条回答
  •  遥遥无期
    2020-12-17 21:15

    Instance variables must be prefixed with an @ and are not accessible from outside. Class variables must be prefixed with an @@.

    The first time you use this variable anywhere in code, it will be initialized. If you want to access the value from outside:

    attr_reader :description
    

    http://rubylearning.com/satishtalim/tutorial.html

提交回复
热议问题