Skip to content

RDoc emits duplicate method-c-new anchors when a class defines both self.new and initialize #1735

@skatkov

Description

@skatkov

I'm working on a rdoc-markdown gem and stumbled into unexpected behavior that seems like a bug to me.

RDoc appears to produce duplicate method anchors when a class defines an explicit singleton new method and also defines initialize.

class DuplicateNewExample
  def self.new(*args, **options, &block)
    super
  end

  def initialize(name)
  end
end

class AliasThenNewExample
  class << self
    alias_method :create, :new

    def new(name)
    end
  end

  def initialize(name, extra = nil)
  end
end

Run:

rdoc duplicate_new.rb
grep -n 'id="method-c-new"' doc/DuplicateNewExample.html
grep -n 'id="method-c-new"' doc/AliasThenNewExample.html

This results in RDoc emitting two id="method-c-new" anchors for each class.

In the "wild" you can find DeprecatedConstantProxy having two "new" methods.
https://api.rubyonrails.org/v8.1.3/classes/ActiveSupport/Deprecation/DeprecatedConstantProxy.html

Or ActiveSupport::TimeZone having two different new methods
https://api.rubyonrails.org/v7.0.0/classes/ActiveSupport/TimeZone.html#method-c-new

Expected result

I'm not 100% sure how to resolve this.

Maybe it's not a good idea to rename #initialize into #new?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions