attr_accessor for array?

后端 未结 4 1809
孤独总比滥情好
孤独总比滥情好 2021-02-19 09:33

I want to have an array as a instance variable using attr_accessor.

But isn\'t attr_accessor only for strings?

How do I use it on an ar

4条回答
  •  眼角桃花
    2021-02-19 10:23

    class SomeObject
      attr_accessor :array
    
      def initialize
        self.array = []
      end
    end
    
    o = SomeObject.new
    
    o.array.push :a
    o.array.push :b
    o.array << :c
    o.array.inspect   #=> [:a, :b, :c]
    

提交回复
热议问题