From: Wayne Blair Date: 2001-05-26T18:40:04+09:00 Subject: [ruby-talk:15749] Re: Ruby Programming Practice > But in a general Ruby programming view, where one programmer > is controlling all the files, is it good practice for a superclass to > use a method (or instance variable) defined by a derived class? > This does not seem to follow from the library designer view. Hi John, This is a fairly common practice when taking advantage of polymorphism. Here are two common examples, one with an instance variable and one with a method. Example 1 ------------------------------------------------------ class FileNode def initialize(parentFolder) @parentFolder end def getParentFolder return @parentFolder end end class FolderNode < FileNode ... end ------------------------------------------------------ Example 2 ------------------------------------------------------ # let's say we're implementing a library to plug projects into an app server # and we want to make a lot of project classes but use DRY principle # and not repeat basic scaffolding code class RequestHandlerBaseClass < RequestHandlerForThirdPartyAppServer def initialize @knowledgeRetrievalSystem = KnowledgeRetrievalSystem.new # common to all projects end def hasPermissions(session) #varies by project # this is a placeholder for an abstract/virtual method end def handleRequest(session, request, outputStream) if hasPermissions(session) then return @knowledgeRetrievalSystem.retrieve(request) else return "Insufficient permissions for request." end end end class DentalProject < RequestHandlerBaseClass ... def hasPermissions(session) return session.getVariable("eligibleForBenefits") end ... end class ManagerInfoProject < RequestHandlerBaseClass ... def hasPermissions(session) return session.getVariable("isManager") end ... end ------------------------------------------------------