inheritance, calling super / initialize

P

Patrick Gundlach

Dear Ruby Hackers,

another newbee question:

--------------------------------------------------
class A
def initialize (a,b)
# do something complicated:
puts a + b
end
end

class B < A
def initialize
super.new ("Hello", "world") # line 12
end
end

B.new
--------------------------------------------------

Gives me
-:12:in `initialize': wrong # of arguments(0 for 2) (ArgumentError)
from -:12:in `initialize'
from -:16:in `new'
from -:16


I can do something like this:

--------------------------------------------------
class B < A
alias :supernew :initialize
def initialize
supernew ("Hello", "world")
end
end
--------------------------------------------------


But is this the way to go? Calling the initialize method for a super
class is common in OOP, isn't it?


Patrick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top