Hello,
I am trying to find a way to preserve the same class name without having
to cut and paste code from one class to another. How do I do something
such as:
test1.rb
class A
def initialize
echo “Initailizing class A.”
end
end
test2.rb
require ‘test1’
class A
def initialize
super
echo “Initializing class A again.”
end
end
a = A.new
Expected output:
Initailizing class A
Initializing class A again.
I know this is not best practice, and that I should be using a different
name for my child class I am deriving off the superclass, but I was just
wanting to know if it is actually doable.
Thanks.
Inheritance does it nicely:
class A
def initalize
puts “Initializing class A”
end
end
class B < A
def initialize
super
puts “Initalizing class B”
end
end
On Jan 10, 2008 8:41 AM, Russell McConnachie [email protected]
wrote:
def initialize
super
I know this is not best practice, and that I should be using a different
name for my child class I am deriving off the superclass, but I was just
wanting to know if it is actually doable.
Thanks.
–
Ryan B.
Feel free to add me to MSN and/or GTalk as this email.
Hi Ryan,
I was actually looking at using the same class name. I didn’t think it
was possible but was wondering. In ruby you can override any method as
you wish dynamically. So defining class A twice with two initialize
functions will not work because the second defined class of the same
name will override the first.
Not quite override, more like extending.
Re-defining a class in Ruby extends that class, but redefining a method
on a
class will override that method, like what you’re finding out now.
On Jan 10, 2008 9:32 AM, Russell McConnachie [email protected]
wrote:
Inheritance does it nicely:
puts “Initalizing class B”
I am trying to find a way to preserve the same class name without
end
end
different
–
Ryan B.
Feel free to add me to MSN and/or GTalk as this email.
On Jan 9, 2:11 pm, Russell McConnachie [email protected] wrote:
I know this is not best practice, and that I should be using a different
name for my child class I am deriving off the superclass, but I was just
wanting to know if it is actually doable.
No, there can only be one class by a given name, and it can’t derive
from itself.
What is your goal?
///ark