# Initialize and superclass

**URL:** <https://www.ruby-forum.com/t/initialize-and-superclass/65708>\
**Category:** Ruby\
**Created:** [August 20, 2006, 6:37pm UTC](https://www.ruby-forum.com/t/initialize-and-superclass/65708 "2006-08-20T18:37:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jvivenot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@jvivenot](https://www.ruby-forum.com/u/jvivenot)\
**Post date:** [August 20, 2006, 6:37pm UTC](https://www.ruby-forum.com/t/initialize-and-superclass/65708/1 "2006-08-20T18:37:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jvivenot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@jvivenot](https://www.ruby-forum.com/u/jvivenot)\
**Post date:** [August 20, 2006, 6:39pm UTC](https://www.ruby-forum.com/t/initialize-and-superclass/65708/2 "2006-08-20T18:39:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jvivenot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@jvivenot](https://www.ruby-forum.com/u/jvivenot)\
**Post date:** [August 20, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/initialize-and-superclass/65708/3 "2006-08-20T18:59:04Z")

</div>

Tilman S. 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:

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

---

<div class="post-metadata">

**Author:** ![jvivenot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@jvivenot](https://www.ruby-forum.com/u/jvivenot)\
**Post date:** [August 21, 2006, 12:25am UTC](https://www.ruby-forum.com/t/initialize-and-superclass/65708/4 "2006-08-21T00:25:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jvivenot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@jvivenot](https://www.ruby-forum.com/u/jvivenot)\
**Post date:** [August 20, 2006, 11:51pm UTC](https://www.ruby-forum.com/t/initialize-and-superclass/65708/5 "2006-08-20T23:51:41Z")

</div>

Wonderful !  
Thanks.

I think there will be many days during which I’ll stay a Ruby-Nuby…

Bye, and thanks everyone

Julien Vivenot

---

<div class="post-metadata">

**Author:** ![jvivenot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@jvivenot](https://www.ruby-forum.com/u/jvivenot)\
**Post date:** [August 21, 2006, 3:37pm UTC](https://www.ruby-forum.com/t/initialize-and-superclass/65708/6 "2006-08-21T15:37:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jvivenot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@jvivenot](https://www.ruby-forum.com/u/jvivenot)\
**Post date:** [August 22, 2006, 12:06am UTC](https://www.ruby-forum.com/t/initialize-and-superclass/65708/7 "2006-08-22T00:06:39Z")

</div>

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
