I have a class that inherits from another. When calling the constructor in the child it is making a call to the parent, which makes a call to a method. For me that should work perfectly fine but I get an exception. The ruby code looks like:
class MyTestClass
def initialize
@foo = "hello world"
puts "init parent"
writeFoo
end
def writeFoo
puts @foo + " from base"
end
end
class MySubClass < MyTestClass
def initialize
puts "init sub"
super
end
def writeFoo
puts @foo + " from sub"
super.writeFoo
end
end
@foo = MySubClass.new
When running that code I get an undefined method exception as below, but the right output is printed. Can someone please explain why?
/Users/tj/dev/coursera/sml/hw6/test.rb:21:in `writeFoo': undefined method `writeFoo' for nil:NilClass (NoMethodError)
from /Users/tj/dev/coursera/sml/hw6/test.rb:5:in `initialize'
from /Users/tj/dev/coursera/sml/hw6/test.rb:16:in `initialize'
from /Users/tj/dev/coursera/sml/hw6/test.rb:25:in `new'
from /Users/tj/dev/coursera/sml/hw6/test.rb:25:in `<main>'
init sub
init parent
hello world from sub
hello world from base
[Finished in 0.1s with exit code 1]