[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Parent class cannot call child class method

Edgardo Hames

1/18/2008 6:13:00 PM

Hi guys,

I have the following design which is not working as I would like it
to:

class A
def bar
raise "Base class can't do bar!"
end

def foo
bar
end
end

class B < A
def bar
puts "B doing bar"
end

def foo
puts "B does this first"
super
end
end

If I do B.new.foo I get an exception because Base class can't do bar
which is understandable. However, I want B#bar to be called. What
would be the proper way to solve this problem?

Thanks in advance,
Edgardo
2 Answers

Stefano Crocco

1/18/2008 6:22:00 PM

0

Alle Friday 18 January 2008, Ed Hames ha scritto:
> Hi guys,
>
> I have the following design which is not working as I would like it
> to:
>
> class A
> def bar
> raise "Base class can't do bar!"
> end
>
> def foo
> bar
> end
> end
>
> class B < A
> def bar
> puts "B doing bar"
> end
>
> def foo
> puts "B does this first"
> super
> end
> end
>
> If I do B.new.foo I get an exception because Base class can't do bar
> which is understandable. However, I want B#bar to be called. What
> would be the proper way to solve this problem?
>
> Thanks in advance,
> Edgardo

It works for me. Are you sure this is the exact code you're running?

Stefano


Edgardo Hames

1/18/2008 6:43:00 PM

0

On Jan 18, 4:21 pm, Stefano Crocco <stefano.cro...@alice.it> wrote:
> Alle Friday 18 January 2008, Ed Hames ha scritto:
>
>
>
> > Hi guys,
>
> > I have the following design which is not working as I would like it
> > to:
>
> > class A
> > def bar
> > raise "Base class can't do bar!"
> > end
>
> > def foo
> > bar
> > end
> > end
>
> > class B < A
> > def bar
> > puts "B doing bar"
> > end
>
> > def foo
> > puts "B does this first"
> > super
> > end
> > end
>
> > If I do B.new.foo I get an exception because Base class can't do bar
> > which is understandable. However, I want B#bar to be called. What
> > would be the proper way to solve this problem?
>
> > Thanks in advance,
> > Edgardo
>
> It works for me. Are you sure this is the exact code you're running?
>

I actually forgot to write the B#bar method in my program :(

Sorry about that,
Edgardo