If you would rather pass no arguments to the parent, use super(). To
control which arguments are passed use this form: super(:passed,
“arguments”, 4, :parent).
I have a class witch inherit from another. I want that the child’s
constructor call the upper constructor before start his (sorry for my
bad english).
Hi David,
You use the #super method. If you don’t need to do any setup in your
class, just leave off the initialize method and it automatically calls
the superclass’s constructor. If you need to do some setup, use the #super method in your subclass’s initialize constructor. If the
arguments to the superclass’s constructor are the same as the
subclass’s, just call super with no arguments. Otherwise pass it the
arguments for the superclass’s constructor.
class C
def initialize(x, y, z) @x, @y, @z = x, y, z
end
end
no setup
class D < C
end
setup but same args to superclass
class D < C
def initialize(x, y, z)
super
end
end
setup but different args to superclass
class D < C
def initialize(x, y, z, a)
super(x, y, z) @a = a
end
end