[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Initialize and superclass

jvivenot

8/20/2006 4:35:00 PM

Hi
I'd like to know something :
Lets say I have two classes A and B, where A is B's superclass. B's
initialize method initialize her own variables, but how could I do in
order to run A.initialize before B.initialize when I write B.new ?
I hope my text is understandable.
Thanks everyone.
Bye

Julien Vivenot

6 Answers

Tilman Sauerbeck

8/20/2006 4:38:00 PM

0

jvivenot [2006-08-21 01:35]:
> Lets say I have two classes A and B, where A is B's superclass. B's
> initialize method initialize her own variables, but how could I do in
> order to run A.initialize before B.initialize when I write B.new ?

If I understood your problem correctly, you're after the "super" method.
Try the following code:

class A
def initialize
puts "initializing instance of A"
end
end

class B
def initialize
super
puts "initializing instance of B"
end
end

Regards,
Tilman

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

e

8/20/2006 4:59:00 PM

0

Tilman Sauerbeck wrote:
> jvivenot [2006-08-21 01:35]:
>> Lets say I have two classes A and B, where A is B's superclass. B's
>> initialize method initialize her own variables, but how could I do in
>> order to run A.initialize before B.initialize when I write B.new ?
>
> If I understood your problem correctly, you're after the "super" method.
> Try the following code:
>
> <snip />

And in the event that you some arguments to B that
A does not know how to handle, use the second form:

class A
def initialize(foo)
puts "initializing instance of A with #{foo}"
end
end

class B
def initialize(foo, bar)
super(foo)
puts "initializing instance of B with #{foo} and #{bar}"
end
end

> Regards,
> Tilman

--
Posted via http://www.ruby-....

jvivenot

8/20/2006 9:45:00 PM

0

Wonderful !
Thanks.

I think there will be many days during which I'll stay a Ruby-Nuby....

Bye, and thanks everyone

Julien Vivenot

Eero Saynatkari wrote:
> Tilman Sauerbeck wrote:
> > jvivenot [2006-08-21 01:35]:
> >> Lets say I have two classes A and B, where A is B's superclass. B's
> >> initialize method initialize her own variables, but how could I do in
> >> order to run A.initialize before B.initialize when I write B.new ?
> >
> > If I understood your problem correctly, you're after the "super" method.
> > Try the following code:
> >
> > <snip />
>
> And in the event that you some arguments to B that
> A does not know how to handle, use the second form:
>
> class A
> def initialize(foo)
> puts "initializing instance of A with #{foo}"
> end
> end
>
> class B
> def initialize(foo, bar)
> super(foo)
> puts "initializing instance of B with #{foo} and #{bar}"
> end
> end
>
> > Regards,
> > Tilman
>
> --
> Posted via http://www.ruby-....

jvivenot

8/20/2006 10:24:00 PM

0

Actually, it seems that I if I write only super because the super
initialize needs no argument, the super method gives every arguments of
the B class to the A initialize, and thus I encounter a ArgumentError.
That's OK
Thanks

Julien Vivenot

jvivenot wrote:
> Wonderful !
> Thanks.
>
> I think there will be many days during which I'll stay a Ruby-Nuby....
>
> Bye, and thanks everyone
>
> Julien Vivenot
>
> Eero Saynatkari wrote:
> > Tilman Sauerbeck wrote:
> > > jvivenot [2006-08-21 01:35]:
> > >> Lets say I have two classes A and B, where A is B's superclass. B's
> > >> initialize method initialize her own variables, but how could I do in
> > >> order to run A.initialize before B.initialize when I write B.new ?
> > >
> > > If I understood your problem correctly, you're after the "super" method.
> > > Try the following code:
> > >
> > > <snip />
> >
> > And in the event that you some arguments to B that
> > A does not know how to handle, use the second form:
> >
> > class A
> > def initialize(foo)
> > puts "initializing instance of A with #{foo}"
> > end
> > end
> >
> > class B
> > def initialize(foo, bar)
> > super(foo)
> > puts "initializing instance of B with #{foo} and #{bar}"
> > end
> > end
> >
> > > Regards,
> > > Tilman
> >
> > --
> > Posted via http://www.ruby-....

Gary Wright

8/21/2006 1:36:00 PM

0


On Aug 20, 2006, at 6:25 PM, jvivenot wrote:

> Actually, it seems that I if I write only super because the super
> initialize needs no argument, the super method gives every
> arguments of
> the B class to the A initialize, and thus I encounter a ArgumentError.
> That's OK
> Thanks

If you don't want to pass any arguments to the superclass's initialize
use this version:

super() # no arguments

So you've got:

super # pass along the arguments to the current method
super() # pass no arguments
super(1,2) # pass these explicit arguments



Gary Wright




jvivenot

8/21/2006 10:00:00 PM

0

Oh, thanks, but actually, I found it by myself, my previous message was
a just a notification of the fact that it would be better to write ()
instead of nothing just after super.
Thanks anyway.
Bye

Julien Vivenot

gwtmp01@mac.com wrote:
> On Aug 20, 2006, at 6:25 PM, jvivenot wrote:
>
> > Actually, it seems that I if I write only super because the super
> > initialize needs no argument, the super method gives every
> > arguments of
> > the B class to the A initialize, and thus I encounter a ArgumentError.
> > That's OK
> > Thanks
>
> If you don't want to pass any arguments to the superclass's initialize
> use this version:
>
> super() # no arguments
>
> So you've got:
>
> super # pass along the arguments to the current method
> super() # pass no arguments
> super(1,2) # pass these explicit arguments
>
>
>
> Gary Wright