is it possible to generate documentation for anonymous classes in rdoc

26 views Asked by At

I would like to document a few methods in an anonymous class for rdoc.

I know that alternative documetation tool exists, such as yard, but not sure the project owners are ready for change they inventory for sake of few methods.

I tried :method: and :doc: directives, but no luck so far

Tried with ruby 2.7, rdoc 6.6.2

# (c) 2024 Big Corporation

# = ExampleModule gives a great example to all of us
#
# This module contains an example of documenting an anonymous class created with Class.new.
module ExampleModule

  # Create an anonymous class using Class.new, almost the same as
  #   class AnonymousClass
  # and give a closure
  # but fails rdoc
  AnonymousClass = Class.new do
    ##
    # :method: initialize
    # This is a initializer with two parameters
    def initialize(param1, param2)
      # ... initialization code ...
    end

    ##
    # :method: example_method
    # == example_method
    #
    # An example method in the anonymous class.
    def example_method
      # ... method implementation ...
    end
  end
end
1

There are 1 answers

0
Serge On

Apparently, if method comments in my example moved before the AnonymousClass, definition, they will appear (in the same HTML page). Of course, class would no be find-able, and some comments are needed to clarify that methods relate to the class, not the module

# (c) 2024 Big Corporation


# This module contains an example of documenting an anonymous class created with Class.new.
module ExampleModule

  # documentation for methods for Anonymous Class
  # is given ahead of class definition to fool rdoc  

    ##
    # :method: an example_method of AnonymousClass
    #     
    # An example method in the anonymous class.

    ##
    # :method: initialize
    # This is an AnonymousClass initializer with two parameters

  # Create an anonymous class using Class.new, almost the same as
  #   class AnonymousClass
  # and give a closure
  # but fails rdoc
  # =class Anonymous Method
  AnonymousClass = Class.new do
    def initialize(param1, param2)
      # ... initialization code ...
    end

    ##
    # :method: example_method
    #     
    # An example method in the anonymous class.
    def example_method
      # ... method implementation ...
    end
  end
end

Note. In the real code I was not even able to add AnonymousClass description to rdoc constant section, but seems to work fine with the above small example. But I guess constant skipping might require this is not necessary related issue.