From: Simen Edvardsen Date: 2006-12-16T23:46:26+09:00 Subject: Re: calling child from parent On 12/16/06, dc wrote: > hi - > > i understand super passes method calls up to the parent. > > is there a way to call down to the child instance, to a different method? > > I realize i could pass a function block up to super, and then use > yield to call back to the child, but i want to hide this complexity > from my child classes. > > the setup is something like this: > > class Webserver < HTTPServlet::AbstractServlet > def do_GET(request, response) > // here i want to hand off to the instance child servlet to > handle the response > child.render() > end > end > Just (re)define render in the child. Example: $ irb irb(main):001:0> class Webserver irb(main):002:1> def get irb(main):003:2> puts "In superclass" irb(main):004:2> render irb(main):005:2> end irb(main):006:1> def render irb(main):007:2> puts "In superclass#render" irb(main):008:2> end irb(main):009:1> end => nil irb(main):010:0> class Servlet < Webserver irb(main):011:1> def render irb(main):012:2> puts "In subclass#render" irb(main):013:2> end irb(main):014:1> end => nil irb(main):015:0> Servlet.new.get In superclass In subclass#render But remember, a superclass doesn't know about its subclasses. > > class Servlet1 < Webserver > def render > // do some stuff > end > end > > etc for other servlets. > > I guess the other way is to instantiate the right child servlet in the > webserver, but this means i dont get access to super methods... > > tx, > > /dc > ------------------------------------------- > David "DC" Collier > mailto:dc@pikkle.com > +81 (0)80 6521 9559 > skype: callto://d3ntaku > ------------------------------------------- > Pikkle 株式会社 > http://www.pikkle.com > ------------------------------------------- > > -- - Simen