Skip to content
Jason Thomas edited this page Jul 17, 2014 · 3 revisions

Why does code before super() in initialize() get executed twice?

Consider this code:

class MyWidget < Qt::Widget
  def initialize(s, parent, name)
    super(parent, name)
    init()
    ...

For a case such as the above, the QWidget can't be instantiated until the initializer has been run up to the point where 'super(parent, name)' is called. Only then, can the number and type of arguments passed to the constructor be known. However, the rest of the initializer can't be run until 'self' is a proper T_DATA object with a wrapped C++ instance.

The solution is to run the initialize code twice. First, only up to the 'super(parent, name)' call, where the QWidget would get instantiated in initialize_qt(). And then rb_throw() jumps out of the initializer returning the wrapped object as a result.

The second time round 'self' will be the wrapped instance of type T_DATA, so initialize() can be allowed to proceed to the end.

Clone this wiki locally