# Could someone please explain what that "super" is?

**URL:** <https://rubytalk.org/t/could-someone-please-explain-what-that-super-is/24554>\
**Category:** ruby-talk\
**Created:** [2 February 2006 09:03 UTC](https://rubytalk.org/t/could-someone-please-explain-what-that-super-is/24554 "2006-02-02T09:03:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rubyist](https://avatars.discourse-cdn.com/v4/letter/r/bbe5ce/32.png) [@Rubyist](https://rubytalk.org/u/Rubyist)\
**Post date:** [2 February 2006 09:03 UTC](https://rubytalk.org/t/could-someone-please-explain-what-that-super-is/24554/1 "2006-02-02T09:03:17Z")

</div>

Hi,

In order not to make an unnecessary crowd here, first I tried to learn  
it myself, but I failed.  
What is that "super" method? Could someone explain it please?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Ross\_Bamford4](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@Ross\_Bamford4](https://rubytalk.org/u/Ross_Bamford4)\
**Post date:** [2 February 2006 09:40 UTC](https://rubytalk.org/t/could-someone-please-explain-what-that-super-is/24554/2 "2006-02-02T09:40:05Z")

</div>

> What is that "super" method? Could someone explain it please?

# The 'super' commentary starts below. This is just a base-class.  
class SomeClass  
&nbsp;&nbsp;def initialize(one, two, three)  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Some: #{[one, two, three].inspect}"  
&nbsp;&nbsp;end

&nbsp;&nbsp;def ameth(one)  
&nbsp;&nbsp;&nbsp;&nbsp;"Some:ameth: #{one}"  
&nbsp;&nbsp;end  
end

# 1. When you override a method, you don't ever have to consider the  
# inherited method if you don't want to.  
class OtherClass \< SomeClass  
&nbsp;&nbsp;def initialize(\*args)  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Other: #{args.inspect}"  
&nbsp;&nbsp;end  
end

# 2. Sometimes, though, you'll want to invoke the inherited method,  
# e.g. to make sure any initialization gets done. For this you  
# use the 'super' keyword, which says 'invoke the inherited method  
# of this name'. Without args, super passes this method's arguments  
# to the superclass method.  
class FurtherClass \< SomeClass  
&nbsp;&nbsp;def initialize(\*args)  
&nbsp;&nbsp;&nbsp;&nbsp;super  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Further: #{args.inspect}"  
&nbsp;&nbsp;end  
end

# 3. If you're changing interfaces (with initialize) you might want to  
# pass up different arguments, which you can do by passing them  
# to super.  
class LastClass \< SomeClass  
&nbsp;&nbsp;def initialize(a1,a2)  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Last: #{[a1,a2].inspect}"  
&nbsp;&nbsp;&nbsp;&nbsp;super(a1,a2,3)  
&nbsp;&nbsp;end

&nbsp;&nbsp;# 3.5. You can of course get the result from super and massage it  
&nbsp;&nbsp;# as you need to when overriding methods.  
&nbsp;&nbsp;def ameth(one)  
&nbsp;&nbsp;&nbsp;&nbsp;s = super('ten')  
&nbsp;&nbsp;&nbsp;&nbsp;"Last:ameth:#{s}"  
&nbsp;&nbsp;end  
end

# 4. You don't \_have\_ to use super. This is mostly equivalent from  
# the user point of view (don't know about internally).  
class Alternative \< SomeClass  
&nbsp;&nbsp;alias :\_\_old\_init :initialize  
&nbsp;&nbsp;def initialize(one,two)  
&nbsp;&nbsp;&nbsp;&nbsp;\_\_old\_init(one,two, 3)  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Alternative: #{[one, two].inspect}"  
&nbsp;&nbsp;end  
end

SomeClass.new(1,2,3)

> > # =\> Some: [1, 2, 3]

OtherClass.new(1,2,3)

> > # =\> Other: [1, 2, 3]

FurtherClass.new(1,2,3)

> > # =\> Some: [1, 2, 3]  
> > # =\> Further: [1, 2, 3]

l = LastClass.new(:one, :two)

> > # =\> Last: [:one, :two]  
> > # =\> Some: [:one, :two, 3]

puts l.ameth(55)

> > # =\> Last:ameth:Some:ameth: ten

Alternative.new(10,20)

> > # =\> Some: [10, 20, 3]  
> > # =\> Alternative: [10, 20]

There's probably more stuff I forgot, but thats the basic gist of it.

> **···**
>
> On Thu, 2006-02-02 at 18:03 +0900, Rubyist wrote:
> 
> --  
> Ross Bamford - rosco@roscopeco.REMOVE.co.uk
