Philippe Lang
2/16/2009 4:26:00 PM
Rick DeNatale wrote:
>> Hi Ruby gurus,
>>=20
>> Let's say we have a Parent class, and a Child1 class that inherits
>> from the Parent class.=20
>>=20
>> 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.=20
------------------------------------
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