Get YARD comments for Ruby objects programmatically

Viewed 286

Given the following ruby file, foo.rb:

# this is a module comment
module A
  # this is a constant comment
  B = 'hi'
  # this is a class comment
  class C
    # this is a method comment
    # @param [String] name Who to say hi to
    # @return [String]
    def self.hi(name)
      "#{B}, #{name}"
    end
  end
end

How can one programmatically get the comments associated with particular objects (e.g. {A::C => 'this is a class comment'}, {B => 'this is a constant comment'})?

I would expect YARD.parse(File.read('/path/to/foo.rb')) or YARD::Parser::SourceParser.parse(File.read('/path/to/foo.rb')) to do something, but they return empty arrays. YARD::Parser::Ruby::RubyParser.parse(File.read('/path/to/foo.rb')) returns a YARD::Parser::Ruby::RipperParser instance that appears to be an AST, but I'd prefer to avoid writing an AST traverser (YARD must have this functionality in order to structure the HTML documentation, but I haven't been able to find it).

(I'm using YARD v0.9.9 in case that's helpful.)

1 Answers
Related