Ruby class instance variables and inheritance

前端 未结 9 659
日久生厌
日久生厌 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:51

    In LibraryBook variable @attributes is a new independent variable, instance variable of object LibraryBook, so its not initialized and you get error "undefined method ... for nil"
    You should to initialize it by LibraryItem attribut's list before using

    class LibraryBook < LibraryItem
      @attributes = LibraryItem::attributes + ['ISBN', 'pages']
    end
    

提交回复
热议问题