Brandon Frohbieter
Brandon Frohbieter

Reputation: 18139

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.new

it is called.

Why?

Upvotes: 1

Views: 775

Answers (1)

peakxu
peakxu

Reputation: 6675

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

Upvotes: 7

Related Questions