rubyyard

YARD How to document block argument type?


Is there a way to avoid creating temporary variables just to document block's argument YARD type?

The snippet below works but these temporary variables doesn't look good.

foos.each_with_object(Set.new) do |foo, obj|
  # @type [Set]
  set = obj
  # now set/memo variable is semi-typed and LSP can do type-based autocompletion
end

Solution

  • Use @param like so:

    # @param foo [Integer]
    # @param set [Set]
    foos.each_with_object(Set.new) do |foo, set|
      # foo & set are LSP/YARD typed in this block
    end
    

    YARD Solargraph @param type

    I'm not sure if it is fully idiomatic YARD approach but it works in LSP/Solargraph.

    Originally I found this example in the Solargraph spec.