Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I document methods where providing a block is optional?

Tags:

ruby

rdoc

yard

I have a function documented like this:

##
# Searches for street names in the local address database. Returns a list
# of strings, or invokes the block for each result.
#
# @param [String, Hash] query
#
#   Can be:
#
#   - A search string with optinal wildcards. Examples:
#     - "Bærumsv*"
#     - "Fornebuve_en"
#
# @param [Integer] limit
#
#   Limits the amount of results. See {#search_street_addresses} for usage
#   examples.
#
# @return [Array<String>]
#
#   A sorted array of street names.
#
# @yield [street_name] Invokes the block with a street name for each
#   result.
#

Yielding this result:

Screenshot

My issue is that the documentation says that the function expects a block and that it returns a value. In reality, the block is optional. If the block is provided, it is invoked for each result and the function returns nothing (nil). If no block is provided, the results are returned in an Array.

How do I make this clear in the documentation? Is there a recommended way?

like image 895
Hubro Avatar asked Apr 30 '14 03:04

Hubro


1 Answers

Use @overload

##
# Searches for street names in the local address database. Returns a list
# of strings, or invokes the block for each result.
#@overload search_street_names(query, limit: nil)
#  @param [String, Hash] query
#
#    Can be:
#
#    - A search string with optinal wildcards. Examples:
#      - "Bærumsv*"
#      - "Fornebuve_en"
#
#  @param [Integer] limit
#
#    Limits the amount of results. See {#search_street_addresses} for usage
#    examples.
#
#  @return [Array<String>]
#
#    A sorted array of street names.
#
#@overload search_street_names(query, limit: nil)
#  @param [String, Hash] query
#
#    Can be:
#
#    - A search string with optinal wildcards. Examples:
#      - "Bærumsv*"
#      - "Fornebuve_en"
#
#  @param [Integer] limit
#
#    Limits the amount of results. See {#search_street_addresses} for usage
#    examples.
#
#  @yield [street_name] Invokes the block with a street name for each
#    result.
##

Returns:

enter image description here

like image 96
SreekanthGS Avatar answered Oct 31 '22 18:10

SreekanthGS