0

如果找不到用户,我有一个返回用户或 nil 的函数。它看起来像这样:

  # 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

我的问题是,如果找不到它,我如何记录它返回用户或 nil?

4

1 回答 1

1

忽略错误条件等代码的行为,在 Yard 中,您只需将替代项添加到注释中:

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

Yard 渲染器会用它做这样的事情:


  • (用户) set_api_user

    拥有访问令牌的用户


小问号是 Yard 的约定,“也许你会得到这个,或者你会得到 nil”

于 2014-06-04T21:33:02.720 回答