Skip to content

#initialize method not called when prepending module onto Ruby class with Java base class #9321

@ccutrer

Description

@ccutrer
class C < java.util.ArrayList
  def initialize
    puts "c"
    super
  end
end

C.new
# =>
# c

module M
  def initialize
    puts "m"
    super
  end
end
C.prepend(M)

C.new
# =>

# I would expect
# =>
# m
# c

Interestingly, if I insert an intermediate class, one of the initialize methods get called:

class C < java.util.ArrayList
  def initialize
    puts "c"
    super
  end
end

C.new
# =>
# c

class D < C
  def initialize
    puts "d"
    super
  end
end

D.new
# =>
# d
# c

module M
  def initialize
    puts "m"
    super
  end
end
D.prepend(M)

D.new
# =>
# c

# I would expect
# =>
# m
# d
# c

In general, the module and the leaf class's initializer are not called if I take it to the next level:

class C < java.util.ArrayList
  def initialize
    puts "c"
    super
  end
end

C.new
# =>
# c

class D < C
  def initialize
    puts "d"
    super
  end
end

D.new
# =>
# d
# c

class E < D
  def initialize
    puts "e"
    super
  end
end

E.new
# =>
# e
# d
# c

module M
  def initialize
    puts "m"
    super
  end
end
E.prepend(M)

E.new
# =>
# d
# c

# I would expect
# =>
# m
# e
# d
# c

If the first base class is not a Java class, all works as expected:

class C
  def initialize
    puts "c"
  end
end

C.new
# =>
# c

class D < C
  def initialize
    puts "d"
    super
  end
end

D.new
# =>
# d
# c

module M
  def initialize
    puts "m"
    super
  end
end
D.prepend(M)

D.new
# =>
# m
# d
# c

JRuby version: jruby 10.0.4.0 (3.4.5) 2026-03-03 9af05b916f OpenJDK 64-Bit Server VM 21.0.10+7-Ubuntu-124.04 on 21.0.10+7-Ubuntu-124.04 +indy +jit [x86_64-linux]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions