# Newbie problem: ArgumentError when creating subclass

**URL:** <https://www.ruby-forum.com/t/newbie-problem-argumenterror-when-creating-subclass/90908>\
**Category:** Ruby\
**Created:** [April 2, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/newbie-problem-argumenterror-when-creating-subclass/90908 "2007-04-02T22:51:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan\_Stevens\_SIAmAI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ad7895/32.png) [@Dan\_Stevens\_SIAmAI](https://www.ruby-forum.com/u/Dan_Stevens_SIAmAI)\
**Post date:** [April 2, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/newbie-problem-argumenterror-when-creating-subclass/90908/1 "2007-04-02T22:51:44Z")

</div>

Could someone explain why the following code raises ArgumentError.  
Thanks.

class SuperClass

def initialize #Make sure you spell ‘initialize’ correctly!  
@my\_attr = 1  
end

attr\_reader :my\_attr  
end

class SubClass \< SuperClass

def initialize(data1, data2)  
super  
@data1 = data1  
@data2 = data2  
end

attr\_reader :data1  
attr\_reader :data2

end

test = SubClass.new(“one”, “two”)  
puts test.my\_attr  
puts test.data1  
puts test.data2

$ ruby inheritance\_problem.rb  
inheritance\_problem.rb:13:in `initialize': wrong number of arguments (2 for 0) (ArgumentError) from inheritance_problem.rb:13:in`initialize’  
from inheritance\_problem.rb:23:in `new’  
from inheritance\_problem.rb:23

---

<div class="post-metadata">

**Author:** ![Dan\_Stevens\_SIAmAI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ad7895/32.png) [@Dan\_Stevens\_SIAmAI](https://www.ruby-forum.com/u/Dan_Stevens_SIAmAI)\
**Post date:** [April 2, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/newbie-problem-argumenterror-when-creating-subclass/90908/2 "2007-04-02T23:00:45Z")

</div>

On 4/2/07, Dan Stevens (IAmAI) [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class SubClass \< SuperClass
> 
> ```
> def initialize(data1, data2)
> super
> 
> ```

This should be super(). Without the parenthesis it defaults to passing  
the arguments given to the current method to the super class’s  
implementation.

Ryan

---

<div class="post-metadata">

**Author:** ![Dan\_Stevens\_SIAmAI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ad7895/32.png) [@Dan\_Stevens\_SIAmAI](https://www.ruby-forum.com/u/Dan_Stevens_SIAmAI)\
**Post date:** [April 2, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/newbie-problem-argumenterror-when-creating-subclass/90908/3 "2007-04-02T23:03:45Z")

</div>

Alle lunedì 2 aprile 2007, Dan Stevens (IAmAI) ha scritto:

> ```
> from inheritance_problem.rb:13:in `initialize'
> from inheritance_problem.rb:23:in `new'
> from inheritance_problem.rb:23
> 
> ```

If you call super with no arguments, it will pass to the superclass  
method all  
the parameters given to the sublcass method. In your case,  
SubClass#initialize takes 2 arguments, while SuperClass#initialize takes  
no  
arguments. Since you call super without arguments, ruby passes both  
arguments  
to SuperClass#initialize, then complains because the number of arguments  
is  
wrong. What you need to do is:

class SubClass \< SuperClass  
def initialize(data1, data2)  
super()  
…  
end  
…  
end

If SuperClass#initialized required one argument, you’d do:

def initialize(data1, data2)  
super(data1)  
…

By the way, you can pass more than one argument to attr\_reader, so you  
can  
write

attr\_reader :data1, :data2

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![Dan\_Stevens\_SIAmAI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ad7895/32.png) [@Dan\_Stevens\_SIAmAI](https://www.ruby-forum.com/u/Dan_Stevens_SIAmAI)\
**Post date:** [April 3, 2007, 12:21am UTC](https://www.ruby-forum.com/t/newbie-problem-argumenterror-when-creating-subclass/90908/4 "2007-04-03T00:21:56Z")

</div>

Hi,

Am Dienstag, 03. Apr 2007, 05:59:31 +0900 schrieb Ryan L.:

> > ```
> > def initialize(data1, data2)
> > super
> > 
> > ```
> 
> This should be super(). Without the parenthesis it defaults to passing  
> the arguments given to the current method to the super class’s  
> implementation.

Ah, parentheses do the trick. I always helped me calling

```
super *[]

```

what is not actually readable or beautiful.

Bertram

---

<div class="post-metadata">

**Author:** ![Dan\_Stevens\_SIAmAI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ad7895/32.png) [@Dan\_Stevens\_SIAmAI](https://www.ruby-forum.com/u/Dan_Stevens_SIAmAI)\
**Post date:** [April 3, 2007, 12:32am UTC](https://www.ruby-forum.com/t/newbie-problem-argumenterror-when-creating-subclass/90908/5 "2007-04-03T00:32:57Z")

</div>

Thanks all of you for clearing that up. In ruby I’m used not using  
parenthesis when calling methods without parameters, but I should be  
able to remember this exception 🙂
