2
我有一個類(ActorClue),其中定義了三個attr_accessor。還有其他類需要的其他常見字段,所以我將這些公共字段放在模塊中(BaseClueProperties)。我在我的ActorClue類中包含該模塊。來自類和包含模塊的屬性列表
下面的代碼採樣:
module BaseClueProperties
attr_accessor :image_url
attr_accessor :summary_text
end
################
class BaseClue
# http://stackoverflow.com/questions/2487333/fastest-one-liner-way-to-list-attr-accessors in-ruby
def self.attr_accessor(*vars)
@attributes ||= []
@attributes.concat vars
super(*vars)
end
def self.attributes
@attributes
end
def attributes
self.class.attributes
end
end
###############
class ActorClue < BaseClue
attr_accessor :actor_name
attr_accessor :movie_name
attr_accessor :directed_by
include BaseClueProperties
.....
end
我實例上面有以下:
>> gg = ActorClue.new
=> #<ActorClue:0x23bf784>
>> gg.attributes
=> [:actor_name, :movie_name, :directed_by]
爲什麼只返回:ACTOR_NAME,:MOVIE_NAME和:directed_by不包括:IMAGE_URL和:summary_text?
我修改了BaseClueProperties閱讀以下內容:
module BaseClueProperties
BaseClue.attr_accessor :image_url
BaseClue.attr_accessor :summary_text
end
但仍具有相同的結果。
對於爲什麼my:image_url和:summary_text屬性未添加到我的@attributes集合有任何想法?