Attr_accessor on class variables

前端 未结 6 1736
Happy的楠姐
Happy的楠姐 2020-12-23 18:57

attr_accessor does not work on the following code. The error says \"undefined method \'things\' for Parent:Class (NoMethodError)\":



        
6条回答
  •  一生所求
    2020-12-23 19:31

    Just some clarification: class variables won't be accessible using attr_accessor. It's all about instance variables:

    class SomeClass
      class << self
        attr_accessor :things
      end
      @things = []
    end
    

    because in Ruby, class is an instance of the class "Class" (God, I love to say that) and attr_accessor sets accessor methods for instance variables.

提交回复
热议问题