If I have a model like this:
class Media
attr_reader :title, :created_at
end
class Video < Media
end
class Picture < Media
attr_reader :image_url
end
So, in the Picture class there is one attribute that should not exist in the superclass. What people usually do when you want to retrieve all the media? Would you want to throw exception on the method image_url? Or how would you re-model it?
If you’re interested in all media, then you shouldn’t care if it’s a video or a picture. In other words, a method that accepts a collection of
Mediashould not call any methods fromMediadescendants. If you need all pictures, then work withPicture, don’t overgeneralize.If you still do need to process all media while telling the types apart, you can branch on a type.
An alternative is to use duck typing. That is, if it has a method
image_url, then it must be some kind of a picture.