Search code examples
rubyrubocop

rubocop linting issue with grape-entity `format_with` method


Strictly a linting error, not a functional error - rubocop raises a linting error on my format_with method. It wants me to do format_with(:mongo_id)(&:to_s) but that is not physically possible.

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

the following is the error message

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


Solution

  • You can do:

    format_with(:mongo_id, &:to_s)
    

    Which is an accepted and working short version for your method(:arg) { |id| id.to_s }.

    That's the syntactic sugar for calling to_proc on every element within the block.