ruby - class instantiation and initialization (initialize is not called?)
class Test
def initialize
puts 'initializing test'
end
end
class TestB < Test
end
something = Class.new(Test)
In the above, the superclass initialize method is not called. If I do
something = TestB.ne开发者_StackOverfloww
it is called.
Why?
Reading the documentation, Class.new(Test) yields a derived class object which has Test as its superclass.
You need to call new on that result to get the printout.
TestA = Class.new(Test)
something_else = TestA.new
精彩评论