开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜