[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

calling child from parent

d c

12/16/2006 5:33:00 AM

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


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....
-------------------------------------------

2 Answers

Ayaz Ahmed Khan

12/16/2006 2:19:00 PM

0

"dc" typed:

> 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 guess the other way is to instantiate the right child servlet in the
> webserver, but this means i dont get access to super methods...

My OO know-how may have turned rusty, but isn't a Parent class (and
therefore its object) oblivous of its children (and therefore the methods
and whatever defined in its children)?

Why not re-define do_GET in Servlet1?

--
Ayaz Ahmed Khan

"Not only is this incomprehensible, but the ink is ugly and the paper
is from the wrong kind of tree."
-- Professor W.

Simen

12/16/2006 2:46:00 PM

0

On 12/16/06, dc <lister@pikkle.com> 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....
> -------------------------------------------
>
>


--
- Simen