【问题标题】:rubocop linting issue with grape-entity `format_with` methodrubocop linting 问题与葡萄实体`format_with` 方法
【发布时间】:2019-06-23 02:30:57
【问题描述】:

严格来说是 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 '&amp;:to_s' as an argument to 'format_with' instead of a block.

【问题讨论】:

    标签: ruby rubocop


    【解决方案1】:

    你可以这样做:

    format_with(:mongo_id, &:to_s)
    

    对于您的method(:arg) { |id| id.to_s },这是一个可接受且有效的简短版本。

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

    【讨论】:

      猜你喜欢
      • 2015-06-13
      • 1970-01-01
      • 2014-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多