使用 grape-entity `format_with` 方法的 rubocop linting 问题

rubocop linting issue with grape-entity `format_with` method

严格来说是 linting 错误,不是功能错误 - rubocop 在我的 format_with 方法上引发了 linting 错误。它想让我做 format_with(:mongo_id)(&:to_s) 但这在物理上是不可能的。

class Mongoid < Grape::Entity
  format_with(:mongo_id) { |id| id.to_s }
  expose :_id, as: :id, format_with: :mongo_id
  # ...
end

以下是报错信息

Style/SymbolProc: Pass '&:to_s' as an argument to 'format_with' instead of a block.

你可以这样做:

format_with(:mongo_id, &:to_s)

您的 method(:arg) { |id| id.to_s }.

可以接受且有效的简短版本

这是在块中的每个元素上调用 to_proc 的语法糖。