attr_accessor for array?

后端 未结 4 1806
孤独总比滥情好
孤独总比滥情好 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:08

    It works for me:

    class Foo
    
      attr_accessor :arr
    
      def initialize() 
        @arr = [1,2,3]
      end
    
    end
    
    f = Foo.new
    p f.arr
    

    Returns the following

    $ ruby /tmp/t.rb
    [1, 2, 3]
    $
    

提交回复
热议问题