Attr_accessor on class variables

前端 未结 6 1741
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:22

    This is probably the simplest way.

    class Parent
      def self.things
        @@things ||= []
      end
    end
    Parent.things << :car
    
    p Parent.things
    

提交回复
热议问题