Ruby class instance variables and inheritance

前端 未结 9 671
日久生厌
日久生厌 2020-12-28 08:02

I have a Ruby class called LibraryItem. I want to associate with every instance of this class an array of attributes. This array is long and looks something lik

9条回答
  •  我在风中等你
    2020-12-28 08:55

    Just as a version:

    class LibraryItem < Object
      def initialize
        @attributes = ['one', 'two'];
      end
    end
    
    class LibraryBook < LibraryItem
      def initialize
       super
       @attributes.push('three')
     end
    end
    
    b = LibraryBook.new
    

提交回复
热议问题