开发者

ruby - class instantiation and initialization (initialize is not called?)

开发者 https://www.devze.com 2023-03-31 09:22 出处:网络
class Test def initialize puts \'initializing test\' end end class TestB < Test end something = Class.new(Test)
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

精彩评论

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