# Inherit and override in the same time?

**URL:** <https://www.ruby-forum.com/t/inherit-and-override-in-the-same-time/160457>\
**Category:** Ruby\
**Created:** [February 16, 2009, 4:30pm UTC](https://www.ruby-forum.com/t/inherit-and-override-in-the-same-time/160457 "2009-02-16T16:30:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![philpraxis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@philpraxis](https://www.ruby-forum.com/u/philpraxis)\
**Post date:** [February 16, 2009, 4:30pm UTC](https://www.ruby-forum.com/t/inherit-and-override-in-the-same-time/160457/1 "2009-02-16T16:30:07Z")

</div>

Hi Ruby gurus,

Let’s say we have a Parent class, and a Child1 class that inherits from  
the Parent class.

I’d like to create a Child2 class that inherits most of the Child1  
class, except for one method, which I’d like to override completely. The  
problem is that I need to call the method in the Parent class, and  
apparently “super.super” is not supported:

* * *

class Parent  
def go  
puts ‘parent’  
end  
end

class Child1 \< Parent  
def go  
super  
puts ‘child1’  
end  
def a\_method\_id\_like\_to\_inherit  
end  
end

class Child2 \< Child1  
def go  
super.super  
puts ‘child2’  
end  
end

## Child2.new.go

## in `go': undefined method`super’ for nil:NilClass (NoMethodError)

I ended up doing this, which apprently works.

* * *

class Parent  
def go  
puts ‘parent’  
end  
end

class Child1 \< Parent  
def go  
super  
puts ‘child1’  
end  
def a\_method\_id\_like\_to\_inherit  
end  
end

class Child2 \< Child1  
def go  
self.class.superclass.superclass.instance\_method( :go ).bind( self  
).call  
puts ‘child2’  
end  
end

## Child2.new.go

## parent child2

Isn’t there anything simpler than that? Or is that kind of pattern  
considered as bad design maybe?

Philippe

---

<div class="post-metadata">

**Author:** ![philpraxis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@philpraxis](https://www.ruby-forum.com/u/philpraxis)\
**Post date:** [February 16, 2009, 4:46pm UTC](https://www.ruby-forum.com/t/inherit-and-override-in-the-same-time/160457/2 "2009-02-16T16:46:06Z")

</div>

> class Child1 \< Parent  
> self.class.superclass.superclass.instance\_method( :go ).bind( self
> 
> Isn’t there anything simpler than that? Or is that kind of pattern  
> considered as bad design maybe?

Smells a little of bad design. Consider putting the common methods for  
Child1, Child2 into a module:

class Parent  
end

class Child1 \< Parent  
include ChildStuff  
end

class Child2 \< Parent  
include ChildStuff  
end

Just a suggestion – it’s hard to really comment on a design without  
knowing  
the problem you’re trying to solve.

---

<div class="post-metadata">

**Author:** ![philpraxis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@philpraxis](https://www.ruby-forum.com/u/philpraxis)\
**Post date:** [February 16, 2009, 4:48pm UTC](https://www.ruby-forum.com/t/inherit-and-override-in-the-same-time/160457/3 "2009-02-16T16:48:28Z")

</div>

On Mon, Feb 16, 2009 at 10:29 AM, Philippe L.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> * * *
> 
> end
> 
> ## puts ‘parent’ end
> 
> ## parent child2
> 
> Isn’t there anything simpler than that? Or is that kind of pattern  
> considered as bad design maybe?

It’s not clear WHY you want to do this but…

your  
self.class.superclass.superclass.instance\_method(:go).bind(self).call

really doesn’t do the same thing, in general as your desired super.super  
(which I’ve not by the way seen in other dynamic OO languages either).

Consider what would happen if you added

class Child3 \< Child2  
end

Child3.new.go

I think that the right way to do this is to refactor the method to  
extract  
just the behavior you want to control, so in this particular case  
something  
like

class Parent  
def go  
puts ‘parent’  
end  
end

class Child1 \< Parent

def put\_me  
puts ‘child1’  
end

def go  
super  
put\_me  
end

end

class Child2 \< Child1  
def put\_me  
puts ‘child2’  
end  
end

class Child3 \< Child2  
end

Parent.new.go  
Child1.new.go  
Child2.new.go  
Child3.new.go

HTH

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)

---

<div class="post-metadata">

**Author:** ![philpraxis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@philpraxis](https://www.ruby-forum.com/u/philpraxis)\
**Post date:** [February 16, 2009, 5:26pm UTC](https://www.ruby-forum.com/t/inherit-and-override-in-the-same-time/160457/4 "2009-02-16T17:26:44Z")

</div>

Rick DeNatale wrote:

> > Hi Ruby gurus,
> > 
> > Let’s say we have a Parent class, and a Child1 class that inherits  
> > from the Parent class.
> > 
> > I’d like to create a Child2 class that inherits most of the Child1  
> > class, except for one method, which I’d like to override completely.  
> > The problem is that I need to call the method in the Parent class,  
> > and apparently “super.super” is not supported:

> It’s not clear WHY you want to do this but…

Hi Rick and James,

Thanks for your answer.

I tried to rewrite my example so it shows a little bit better WHY I’m  
trying to do that.

* * *

class Form  
def initialize  
puts “form preparation”  
end  
def search\_data  
puts “form search preparation”  
yield  
puts “form search cleanup”  
end  
end

class ProductForm \< Form  
def initialize  
super  
puts “product form widgets preparation”  
end  
def search\_data  
super do  
puts “product search”  
end  
end  
end

class ArchivedProductForm \< ProductForm  
def search\_data  
self.class.superclass.superclass.instance\_method( :search\_data  
).bind( self ).call do  
puts “archived product search”  
end  
end  
end

ProductForm.new.search\_data  
puts “------------------”  
ArchivedProductForm.new.search\_data

* * *

## form preparation product form widgets preparation form search preparation product search form search cleanup

## form preparation product form widgets preparation form search preparation archived product search form search cleanup

Basically, a ProductForm or ArchivedProductForm are identical, except  
the way they are being searched for. All the rest, like the widgets on  
the form, are exactly the same.

I’m pretty sure now I have to refactor somehow my code, to keep things  
clean. I guess the following code, with an extra class, is the way to  
go:

* * *

class Form  
def initialize  
puts “form preparation”  
end  
def search\_data  
puts “form search preparation”  
yield  
puts “form search cleanup”  
end  
end

class ProductBaseForm \< Form  
def initialize  
super  
puts “product form widgets preparation”  
end  
end

class ProductForm \< ProductBaseForm  
def search\_data  
super do  
puts “product search”  
end  
end  
end

class ArchivedProductForm \< ProductBaseForm  
def search\_data  
super do  
puts “archived product search”  
end  
end  
end

ProductForm.new.search\_data  
puts “------------------”  
ArchivedProductForm.new.search\_data

* * *

## form preparation product form widgets preparation form search preparation product search form search cleanup

## form preparation product form widgets preparation form search preparation archived product search form search cleanup

Best regards,

Philippe

---

<div class="post-metadata">

**Author:** ![philpraxis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@philpraxis](https://www.ruby-forum.com/u/philpraxis)\
**Post date:** [February 16, 2009, 10:55pm UTC](https://www.ruby-forum.com/t/inherit-and-override-in-the-same-time/160457/5 "2009-02-16T22:55:47Z")

</div>

On 16.02.2009 16:29, Philippe L. wrote:

> * * *
> 
> end
> 
> ## puts ‘parent’ end
> 
> ## parent child2
> 
> Isn’t there anything simpler than that? Or is that kind of pattern  
> considered as bad design maybe?

IMHO the “bug” is in your inheritance design. You probably should  
rather do

class Parent  
def go  
puts ‘parent’  
end  
end

class BaseChild \< Parent  
def a\_method\_id\_like\_to\_inherit  
puts ‘Boo!’  
end  
end

class Child1 \< BaseChild  
def go  
super  
puts ‘child1’  
end  
end

class Child2 \< BaseChild  
def go  
super  
puts ‘child2’  
end  
end

Now, if there are no other classes that should derive from Parent you  
might as well merge BaseChild and Parent.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![philpraxis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@philpraxis](https://www.ruby-forum.com/u/philpraxis)\
**Post date:** [February 16, 2009, 11:17pm UTC](https://www.ruby-forum.com/t/inherit-and-override-in-the-same-time/160457/6 "2009-02-16T23:17:06Z")

</div>

On Mon, Feb 16, 2009 at 1:28 PM, Pit C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> def search\_data  
> end  
> class ArchivedProductForm \< ProductForm  
> def product\_search  
> puts “archived product search”  
> end  
> end

Exactly!

This is what I recommended, recast into the “new” problem.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)

---

<div class="post-metadata">

**Author:** ![philpraxis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@philpraxis](https://www.ruby-forum.com/u/philpraxis)\
**Post date:** [February 16, 2009, 7:31pm UTC](https://www.ruby-forum.com/t/inherit-and-override-in-the-same-time/160457/7 "2009-02-16T19:31:05Z")

</div>

2009/2/16 Philippe L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I tried to rewrite my example so it shows a little bit better WHY I’m  
> trying to do that.

Philippe, you could introduce a new method, for example #product\_search:

class Form  
def initialize  
puts “form preparation”  
end  
def search\_data  
puts “form search preparation”  
yield  
puts “form search cleanup”  
end  
end

class ProductForm \< Form  
def initialize  
super  
puts “product form widgets preparation”  
end  
def search\_data  
super do  
product\_search  
end  
end  
def product\_search  
puts “product search”  
end  
end

class ArchivedProductForm \< ProductForm  
def product\_search  
puts “archived product search”  
end  
end

(not tested)

Regards,  
Pit
