【发布时间】:2017-12-28 18:03:00
【问题描述】:
我在 .rb 类文件中有这段代码:
field :user, Types::UserType do resolve ->(_obj, _args, ctx) { ctx[:user] } end
我需要的是使用 {} 从单行中删除 do 和 end。
[Style/BlockDelimiters] Prefer{...}overdo...endfor single-line blocks.
但我不明白为什么会抛出这个错误:
[ruby] syntax error, unexpected '{', expecting '('
[ruby] syntax error, unexpected '}', expecting keyword_end
RuboCop 版本
$ rubocop -V
0.51.0 (using Parser 2.4.0.2, running on ruby 2.4.2 x64-mingw32)
【问题讨论】:
-
“我需要的是删除”——你不需要盲目地遵循 rubocop 一开始所说的一切。如果您仍然想满足它,请正确管理运算符优先级,可能在
Types::UserType周围加上括号。 -
@mudasobwa 我认为它会围绕整个参数列表,例如
field(:user, Types::UserType) do #etc据说 2 行或使用更自然的内联块语法会更好。 -
field(:user, Types::UserType) { resolve ->(_obj, _args, ctx) { ctx[:user] } } -
@engineersmnky,
more natural inline block syntax是什么? -
见@gabrielhilal 评论。使用花括号
{...}来表示内联块更自然(阅读惯用语)。内部解析器不允许使用do...end作为内联块,它通常不被认为是正确的样式