Sha256: 50e4ff3ce97ca7124b97f2f9238fc084a2327ffd3e46c028f2c7a7dc7c321bfd

Contents?: true

Size: 1.13 KB

Versions: 10

Compression:

Stored size: 1.13 KB

Contents

# Subclassing

Creating a Ruby subclass of one of the GObject classes requires following some rules.

First of all, if you override the initializer, you **must** call `super`
otherwise, the underlying GObject pointer will not be created and stored.

Second, GObject objects often have several constructors. GirFFI creates a
separate initializer for each constructor. This allows customization of the
call to super for each initializer. This means you will have to override each
initializer separately, as needed.

As an example, here is a subclass of `Regress::TestSubObj`, a class from
GObjectIntrospection's test suite, adding an extra argument to some of its
constructors.

```
class MyObj < Regress::TestSubObj
  attr_reader :animal

  def initialize animal
    super()
    @animal = animal
  end

  def initialize_from_file animal, file
    super(file)
    @animal = animal
  end
end

o1 = MyObj.new 'dog'
o1.foo                                          # => 'dog'
o2 = MyObj.constructor
o2.foo                                          # => nil
o3 = MyObj.new_from_file 'cat', 'my_file.txt'
o3.foo                                          # => 'cat'
```

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
gir_ffi-0.18.0 docs/Subclassing.md
gir_ffi-0.17.0 docs/Subclassing.md
gir_ffi-0.16.1 docs/Subclassing.md
gir_ffi-0.16.0 docs/Subclassing.md
gir_ffi-0.15.9 docs/Subclassing.md
gir_ffi-0.15.8 docs/Subclassing.md
gir_ffi-0.15.7 docs/Subclassing.md
gir_ffi-0.15.6 docs/Subclassing.md
gir_ffi-0.15.5 docs/Subclassing.md
gir_ffi-0.15.4 docs/Subclassing.md