How to document a function that returns a specific object defined by the app in yard?

StackOverflow https://stackoverflow.com//questions/24047627

  •  21-12-2019
  •  | 
  •  

Frage

I have a function that returns a User or nil if it can't find the user. It looks like this:

  # Given an access token in the HTTP headers, it returns the User who owns the token
  #
  # @return [User] the user who owns the access token
  def set_api_user
    token = /(.*)=\"(.*)\"/.match(request.headers["Authorization"])[2]
    @api_user = ApiKey.find_by(access_token: token).user
  end

My question is, how do I document that it returns a User or nil if it can't find it?

War es hilfreich?

Lösung

Ignoring the behaviour of the code for error conditions etc, in Yard, you just add the alternatives into the comment:

# @return [User,nil] the user who owns the access token

The Yard renderer will do something like this with it:


  • (User?) set_api_user

    the user who owns the access token


The little question mark is Yard's convention for "maybe you'll get this, or maybe you'll get nil"

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top