YARD documentation for a method that returns a class
Asked Answered
H

2

6

I need to tag the @return type of a method that returns a class instead of an instance. Example:

# @return [String]  # This implies that the return type is a String object
def string_class
  String            # The method actually returns the String class itself
end

Does YARD have a standard for this? My first guess was @return [Class<String>] but I haven't been able to find documentation for anything like it.

Heng answered 22/5, 2017 at 7:8 Comment(1)
I'd simply write @return [Class]Kekkonen
H
8

According to the YARD team themselves, the preferred way to document the provided example is Class<String>. Reference: https://github.com/lsegal/yard/issues/1109

Heng answered 3/9, 2017 at 13:49 Comment(1)
Thank you! I had been searching for this.Henninger
J
2

In ruby, (almost) everything is an object, including classes themselves!

String.class == Class

(There is a class called Class, of which String is an instance.) Therefore, you can document the method like this:

# @return [Class]
def string_class
  String
end
Jeopardous answered 22/5, 2017 at 7:29 Comment(2)
I know I can use Class, but I'd like to document it as the String class specifically, if possible. The goal is to use the YARD API to find the return type's methods. In this example, I'd want the results to include String.try_convert.Heng
Ahh right, I see. I don't know if that's possible in YARD syntax (I'd have to research some more...), but you could also consider documenting this as a duck-typed method that the returned object should respond to: @return [#try_convert].Jeopardous

© 2022 - 2024 — McMap. All rights reserved.